You are here

public function RiddlerTestCase::testRiddlesAdministration in Captcha Riddler 8

Method for testing the Riddler riddles administration.

File

tests/src/Functional/RiddlerTestCase.php, line 51

Class

RiddlerTestCase
Tests Riddler main test case sensitivity.

Namespace

Drupal\Tests\riddler\Functional

Code

public function testRiddlesAdministration() {
  $form_values_1_question = [
    'riddler[0][question]' => 'What is your favorite primary color?',
    'riddler[0][response]' => 'red, blue, yellow',
  ];
  $form_values_2_question = [
    'riddler[0][question]' => 'What is your favorite primary color?',
    'riddler[0][response]' => 'red, blue, yellow',
    'riddler[1][question]' => 'What color is Druplicon?',
    'riddler[1][response]' => 'blue',
  ];
  $addAnotherButton = 'Add another riddle';

  // Check that the admin form exists.
  $this
    ->drupalGet(self::RIDDLER_ADMIN_PATH);
  $this
    ->assertText(t('Add questions that you require users to answer.'), 'Admin should be able to see the Riddler admin form.', 'Riddler');

  // This creates a new set of question and response.
  $ajax = $this
    ->drupalPostForm(self::RIDDLER_ADMIN_PATH, $form_values_1_question, $addAnotherButton);
  $this
    ->assertText(t('Riddle 2'), 'Admin should be able to ajax add a new riddle row.', 'Riddler');
  $this
    ->drupalPostForm(NULL, $form_values_2_question, 'Save configuration');
  $this
    ->assertFieldByName('riddler[1][question]', t('What color is Druplicon?'), 'Admin should be able to add a new question.', 'Riddler');
  $this
    ->assertFieldByName('riddler[1][response]', t('blue'), 'Admin should be able to add a new response.', 'Riddler');

  // This deletes a set of question and response.
  $ajax = $this
    ->drupalPostForm(self::RIDDLER_ADMIN_PATH, $form_values_2_question, 'riddle-remove-1');
  $this
    ->assertNoFieldByName('riddler[1][question]', NULL, 'Admin should be able to ajax remove a new question.', 'Riddler');
  $this
    ->assertNoFieldByName('riddler[1][response]', NULL, 'Admin should be able to ajax remove a new response.', 'Riddler');

  // Save the config form without the deleted riddle.
  $this
    ->drupalPostForm(NULL, $form_values_1_question, 'Save configuration');
  $this
    ->assertNoText(t('Riddle 2'), 'Admin should be able to save without a deleted riddle.', 'Riddler');
  $this
    ->assertNoFieldByName('riddler[1][question]', NULL, 'Admin should be able to save to remove a new question.', 'Riddler');
  $this
    ->assertNoFieldByName('riddler[1][response]', NULL, 'Admin should be able to save to remove a new response.', 'Riddler');
}