Skip to content

Commit

Permalink
Merge pull request #7675 from Automattic/fix/timed-quiz-input-not-sto…
Browse files Browse the repository at this point in the history
…pping-when-time-ends

Fix timed quiz still working for students even after time ends
  • Loading branch information
Imran92 authored Nov 7, 2024
2 parents 6f39f3b + b54aec3 commit 745f967
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 3 deletions.
3 changes: 3 additions & 0 deletions assets/blocks/quiz/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.sensei-quiz-action--hidden {
display: none;
}
4 changes: 4 additions & 0 deletions changelog/fix-timed-quiz-input-not-stopping-when-time-ends
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Timed quiz not stopping when time ends
22 changes: 22 additions & 0 deletions includes/class-sensei-quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ public function __construct( $file = __FILE__ ) {
// Create the quiz progress when the student visits the quiz page for the first time.
add_action( 'wp', array( $this, 'maybe_create_quiz_progress' ) );
}

// Enqueue frontend styles.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
}

/**
* Enqueue frontend CSS files for quizzes.
*
* @internal
*
* @since $$next-version$$
* @return void
*/
public function enqueue_styles() {
if (
is_admin() ||
! is_singular( 'quiz' )
) {
return;
}

Sensei()->assets->enqueue( 'sensei-single-quiz-style', 'blocks/quiz/style.css' );
}

/**
Expand Down
5 changes: 3 additions & 2 deletions templates/single-quiz/pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ class="wp-block-button__link button sensei-stop-double-submission sensei-quiz-pa
<?php echo esc_attr( _n( 'Next Question', 'Next Questions', $sensei_next_question_count, 'sensei-lms' ) ); ?>
</button>
</div>
<?php elseif ( $sensei_is_quiz_available && ! $sensei_is_quiz_completed ) : ?>
<div class="sensei-quiz-action wp-block-button">
<?php endif ?>
<?php if ( $sensei_is_quiz_available && ! $sensei_is_quiz_completed ) : ?>
<div class="sensei-quiz-action<?php echo esc_attr( $sensei_question_loop['current_page'] < $sensei_question_loop['total_pages'] ? '--hidden' : '' ); ?> wp-block-button">
<button
type="submit"
name="quiz_complete"
Expand Down
106 changes: 106 additions & 0 deletions tests/unit-tests/blocks/course-theme/test-class-quiz-actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* This file contains the Quiz_Actions_Test class.
*
* @package sensei
*/

use Sensei\Blocks\Course_Theme\Quiz_Actions;

/**
* Tests for Quiz_Actions class.
*
* @covers Sensei\Blocks\Course_Theme\Quiz_Actions
*/
class Quiz_Actions_Test extends WP_UnitTestCase {

use Sensei_Course_Enrolment_Test_Helpers;
use Sensei_Course_Enrolment_Manual_Test_Helpers;

/**
* Sensei factory instance.
*
* @var $factory
*/
protected $factory;

public function setUp(): void {
parent::setUp();

$this->factory = new Sensei_Factory();

self::resetEnrolmentProviders();
}

public function tearDown(): void {
parent::tearDown();
$this->factory->tearDown();

// Remove all lessons.
$lessons = get_posts( 'post_type=lesson' );
foreach ( $lessons as $index => $lesson ) {
wp_delete_post( $lesson->ID, true );
}

// Remove all quizzes.
$quizzes = get_posts( 'post_type=quiz' );
foreach ( $quizzes as $index => $quiz ) {
wp_delete_post( $quiz->ID, true );
}
WP_Block_Supports::$block_to_render = null;

self::resetEnrolmentProviders();
}

public function testRender_WhenPaginationIsShown_AlwaysRendersQuizCompleteButton() {
/* Arrange */
global $post, $sensei_question_loop;

$this->addEnrolmentProvider( Sensei_Test_Enrolment_Provider_Always_Provides::class );
$this->prepareEnrolmentManager();

$user_id = $this->factory->user->create();
wp_set_current_user( $user_id );

$course_id = $this->factory->course->create();
$lesson_id = $this->factory->lesson->create(
[
'meta_input' => [
'_lesson_course' => $course_id,
],
]
);
$quiz_id = $this->factory->maybe_create_quiz_for_lesson( $lesson_id );
$post = get_post( $quiz_id );

$this->factory->question->create_many( 10, [ 'quiz_id' => $quiz_id ] );

$course_enrolment = Sensei_Course_Enrolment::get_course_instance( $course_id );
$course_enrolment->enrol( $user_id );

update_post_meta(
$quiz_id,
'_pagination',
wp_json_encode( [ 'pagination_number' => 2 ] )
);

$this->go_to( get_permalink( $quiz_id ) );

WP_Block_Supports::$block_to_render = [
'attrs' => [],
'blockName' => 'sensei-lms/quiz-actions',
];

Sensei_Quiz::start_quiz_questions_loop();

/* Act */
$result_for_other_pages = ( new Quiz_Actions() )->render();
$sensei_question_loop['current_page'] = $sensei_question_loop['total_pages'];
$result_for_last_page = ( new Quiz_Actions() )->render();

/* Assert */
$this->assertStringContainsString( 'sensei-quiz-action--hidden', $result_for_other_pages );
$this->assertStringNotContainsString( 'sensei-quiz-action--hidden', $result_for_last_page );
}
}
46 changes: 45 additions & 1 deletion tests/unit-tests/test-class-quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Sensei\Internal\Student_Progress\Quiz_Progress\Repositories\Tables_Based_Quiz_Progress_Repository;

class Sensei_Class_Quiz_Test extends WP_UnitTestCase {

use Sensei_Test_Login_Helpers;
use Sensei_Test_Redirect_Helpers;

/**
Expand Down Expand Up @@ -2539,4 +2539,48 @@ public function testIsQuizAwaitingGradeForUser_IsNotUngraded_ReturnsFalse() {
/* Reset */
Sensei()->quiz_progress_repository = $quiz_progress_repository;
}

public function testEnqueueStyles_WhenNoPageSet_DoesNotEnqueueStyles() {
/* Act */
Sensei()->quiz->enqueue_styles();

/* Assert */
$this->assertFalse( wp_style_is( 'sensei-single-quiz-style' ) );
}

public function testEnqueueStyles_WhenOnPagesOtherThanQuiz_DoesNotEnqueueStyles() {
/* Arrange */
$lesson_id = $this->factory->lesson->create();

/* Act */
$this->go_to( get_permalink( $lesson_id ) );
Sensei()->quiz->enqueue_styles();

/* Assert */
$this->assertFalse( wp_style_is( 'sensei-single-quiz-style' ) );
}

public function testConstructor_WhenInAdminScreens_DoesNotHookEnqueueStyles() {
/* Arrange */
$this->login_as_admin();

/* Act */
set_current_screen( 'edit.php' );
Sensei()->quiz->enqueue_styles();

/* Assert */
$this->assertFalse( wp_style_is( 'sensei-single-quiz-style' ) );
}

public function testEnqueueStyles_WhenOnQuizPage_EnqueueStyles() {
/* Arrange */
$quiz_id = $this->factory->quiz->create();

/* Act */
$this->go_to( get_permalink( $quiz_id ) );
Sensei()->quiz->enqueue_styles();

/* Assert */
$this->assertTrue( wp_style_is( 'sensei-single-quiz-style' ) );
}
}
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const files = [
'blocks/quiz/index.js',
'blocks/quiz/ordering-promo/index.js',
'blocks/quiz/quiz.editor.scss',
'blocks/quiz/style.scss',
'blocks/shared.js',
'blocks/shared-style.scss',
'blocks/shared-style-editor.scss',
Expand Down

0 comments on commit 745f967

Please sign in to comment.