Commit a46da155 authored by Alexey Beloglazov's avatar Alexey Beloglazov Committed by Ev Maslovskiy
Browse files

Issue #3313777: Cover questions in lessons with tests

parent c615976b
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Display "loading indicator" only if a user have permissions to tick/un-tick checklist items.
- #3312217: Cover creation of lessons with tests.
- #3313461: Cover quizzes with tests.
- #3313777: Cover questions in lessons with tests.

## [2.9.0]
- #3312107: Highlighting should be applied for whole words.
+199 −6
Original line number Diff line number Diff line
@@ -71,6 +71,159 @@ class AssessmentTest extends WebDriverTestBase {
   */
  protected const QUESTION_SCALE = 'Question: Scale';

  /**
   * Test the lesson with questions.
   */
  public function testLessonWithQuestions() {
    $account = $this->drupalCreateUser([], 'test', TRUE);
    $this->drupalLogin($account);

    $assert = $this->assertSession();
    $page = $this->getSession()->getPage();

    // 1. Create a quiz.
    $this->drupalGet('node/add/module_lesson');

    $page->fillField('Title', 'Demo lesson');

    // Set alias.
    $page->findById('edit-path-0')->click();
    $page->fillField('URL alias', '/demo-lesson');

    // Add all types of questions (1 per page, 2 on the last page).
    $this->addBlock(static::QUESTION_SHORT_ANSWER);
    $this->addPage();
    $this->addBlock(static::QUESTION_LONG_ANSWER);
    $this->addPage();
    $this->addBlock(static::QUESTION_SINGLE_CHOICE);
    $this->addPage();
    $this->addBlock(static::QUESTION_MULTIPLE_CHOICE);
    $this->addPage();
    $this->addBlock(static::QUESTION_SCALE);
    $this->addPage();
    $this->addBlock(static::QUESTION_SHORT_ANSWER);
    $this->addBlock(static::QUESTION_SHORT_ANSWER, 'business');

    // Save.
    $page->pressButton('Save');

    // 2. Fill the question on 1st page and submit it.
    $shortAnswerField = $this->getQuestionField('How are you?');
    $this->assertNotEmpty($shortAnswerField);
    $shortAnswerField->setValue('Bad');

    // Submit question.
    $this->submitQuestion();

    // Make sure that input has disabled and "suggested answer" has appeared.
    $this->assertTrue($shortAnswerField->hasAttribute('disabled'));
    $suggestedAnswer = $page->find('css', 'strong:contains("Suggested answer")');
    $this->assertSame('Suggested answer Fine', $suggestedAnswer->getParent()->getText());

    // Go to the next step.
    $nextButton = $assert->waitForElementVisible('css', '#anu-application button[data-test=anu-lms-navigation-next]');
    $nextButton->click();

    // 3. Fill the question on 2nd page and submit it.
    $longAnswerField = $this->getQuestionField('How is the weather?', FALSE, 'textarea');
    $this->assertNotEmpty($longAnswerField);
    $longAnswerField->setValue('It is rainy.');

    // Submit question.
    $this->submitQuestion();

    // Make sure that input has disabled and "suggested answer" has appeared.
    $this->assertTrue($longAnswerField->hasAttribute('disabled'));
    $suggestedAnswer = $page->find('css', 'strong:contains("Suggested answer")');
    $this->assertSame('Suggested answer It is sunny.', $suggestedAnswer->getParent()->getText());

    // Go to the next step.
    $nextButton = $assert->waitForElementVisible('css', '#anu-application button[data-test=anu-lms-navigation-next]');
    $nextButton->click();

    // 4. Fill the question on 3rd page and submit it.
    $singleChoiceField = $this->getQuestionField('Which OS is older?', TRUE);
    $this->assertSame(2, count($singleChoiceField));
    $singleChoiceField[1]->click();

    // Submit question.
    $this->submitQuestion();

    // Make sure that radio buttons have disabled and have correct states.
    $this->assertTrue($singleChoiceField[0]->hasAttribute('disabled'));
    $this->assertTrue($singleChoiceField[1]->hasAttribute('disabled'));
    $singleChoice1Wrapper = $page->find('css', 'span[data-radio-option="Which OS is older?:0"]');
    $singleChoice2Wrapper = $page->find('css', 'span[data-radio-option="Which OS is older?:1"]');
    $this->assertSame('anu-lms-radio-correct-error', $singleChoice1Wrapper->getAttribute('data-test'));
    $this->assertSame('anu-lms-radio-incorrect-error', $singleChoice2Wrapper->getAttribute('data-test'));

    // Go to the next step.
    $nextButton = $assert->waitForElementVisible('css', '#anu-application button[data-test=anu-lms-navigation-next]');
    $nextButton->click();

    // 5. Fill the question on 4th page and submit it.
    $multipleChoiceField = $this->getQuestionField('Which languages do you know?', TRUE);
    $this->assertSame(2, count($multipleChoiceField));
    $multipleChoiceField[0]->check();
    $multipleChoiceField[1]->check();

    // Submit question.
    $this->submitQuestion();

    // Make sure that radio buttons have disabled and have correct states.
    $this->assertTrue($multipleChoiceField[0]->hasAttribute('disabled'));
    $this->assertTrue($multipleChoiceField[1]->hasAttribute('disabled'));
    $multipleChoice1Wrapper = $page->find('css', 'span[data-checkbox-option="Which languages do you know?:0"]');
    $multipleChoice2Wrapper = $page->find('css', 'span[data-checkbox-option="Which languages do you know?:1"]');
    $this->assertSame('anu-lms-checkbox-success', $multipleChoice1Wrapper->getAttribute('data-test'));
    $this->assertSame('anu-lms-checkbox-success', $multipleChoice2Wrapper->getAttribute('data-test'));

    // Go to the next step.
    $nextButton = $assert->waitForElementVisible('css', '#anu-application button[data-test=anu-lms-navigation-next]');
    $nextButton->click();

    // 6. Fill the question on 5th page and submit it.
    $scaleField = $this->getQuestionField('How many child had Franklin?');
    $this->assertNotEmpty($scaleField);

    // Set value to "3" for "material-ui slider item".
    $jsScript = <<<JS
      const slider = document.querySelector('.MuiSlider-root');
      const fiberKey = Object.keys(slider).find((key) => key.startsWith('__reactFiber$'));
      slider[fiberKey].return.memoizedProps.onChange(null, 3);
    JS;

    $this
      ->getSession()
      ->executeScript($jsScript);

    // Submit question.
    $this->submitQuestion();

    // Make sure that radio buttons have disabled and have correct states.
    $this->assertSame('3', $scaleField->getAttribute('value'));

    // Go to the next step.
    $nextButton = $assert->waitForElementVisible('css', '#anu-application button[data-test=anu-lms-navigation-next]');
    $nextButton->click();

    // 7. Fill the question on 6th page and submit it.
    $shortAnswer1Field = $this->getQuestionField('How are you?');
    $shortAnswer2Field = $this->getQuestionField('How is your business?');
    $shortAnswer1Field->setValue('Bad');
    $shortAnswer2Field->setValue('Well');

    // Submit question.
    $this->submitQuestion();

    // Make sure that inputs have disabled and "suggested answer" have appeared.
    $this->assertTrue($shortAnswerField->hasAttribute('disabled'));
    $suggestedAnswers = $page->findAll('css', 'strong:contains("Suggested answer")');
    $this->assertSame(2, count($suggestedAnswers));
    $this->assertSame('Suggested answer Fine', $suggestedAnswers[0]->getParent()->getText());
    $this->assertSame('Suggested answer Fine', $suggestedAnswers[1]->getParent()->getText());
  }

  /**
   * Test the assessments features.
   */
@@ -294,15 +447,14 @@ class AssessmentTest extends WebDriverTestBase {
   *
   * @param string $type
   *   Type of block.
   * @param string|null $option
   *   Option of "values" for block.
   */
  private function addBlock($type) {
  private function addBlock($type, $option = NULL) {
    $assert = $this->assertSession();
    $page = $this->getSession()->getPage();

    // Add Block.
    $page
      ->find('css', '[data-drupal-selector="edit-field-module-assessment-items-add-more-browse"]')
      ->click();
    $this->findLastButton('Add block')->click();
    $assert->assertWaitOnAjaxRequest();

    // Choose the requested paragraph.
    $assert
@@ -312,7 +464,13 @@ class AssessmentTest extends WebDriverTestBase {

    // Fill values for a block.
    if ($type === static::QUESTION_SHORT_ANSWER) {
      if ($option === 'business') {
        $this->findLastField('Question')->setValue('How is your business?');
      }
      else {
        $this->findLastField('Question')->setValue('How are you?');
      }

      $this->findLastField('Correct answer')->setValue('Fine');
    }
    elseif ($type === static::QUESTION_LONG_ANSWER) {
@@ -354,4 +512,39 @@ class AssessmentTest extends WebDriverTestBase {
    }
  }

  /**
   * Add a page.
   */
  private function addPage() {
    $assert = $this->assertSession();
    $this->findLastButton('Add Page')->click();
    $assert->assertWaitOnAjaxRequest();
  }

  /**
   * Submit the question.
   */
  private function submitQuestion() {
    $page = $this->getSession()->getPage();
    $assert = $this->assertSession();

    $nextButton = $assert->waitForElementVisible('css', '#anu-application button[data-test=anu-lms-navigation-next]');

    // Make sure "Next button" has disabled.
    if ($nextButton) {
      $this->assertTrue($nextButton->hasClass('Mui-disabled'));
    }

    while ($submitButton = $page->find('css', '#anu-application button[data-test=anu-lms-quiz-submit]')) {
      $submitButton->click();
      $assert->waitForElementRemoved('css', '#anu-application button[data-test=anu-lms-quiz-submit]');
    }

    // Make sure "Next button" has enabled.
    if ($nextButton) {
      $nextButton = $assert->waitForElementVisible('css', '#anu-application button[data-test=anu-lms-navigation-next]:not(.Mui-disabled)');
      $this->assertNotEmpty($nextButton);
    }
  }

}