You are here

RiddlerTestCase.php in Captcha Riddler 8

File

tests/src/Functional/RiddlerTestCase.php
View source
<?php

namespace Drupal\Tests\riddler\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;

/**
 * Tests Riddler main test case sensitivity.
 *
 * @group captcha
 */
class RiddlerTestCase extends BrowserTestBase {
  use UserCreationTrait;

  /**
   * Drupal path of the Riddler admin page.
   */
  const RIDDLER_ADMIN_PATH = 'admin/config/people/captcha/riddler';

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'block',
    'comment',
    'captcha',
    'riddler',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * A test user with administrative privileges.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->adminUser = $this
      ->drupalCreateUser([
      'administer CAPTCHA settings',
    ]);
    $this
      ->drupalLogin($this->adminUser);
  }

  /**
   * Method for testing the Riddler riddles administration.
   */
  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');
  }

}

Classes

Namesort descending Description
RiddlerTestCase Tests Riddler main test case sensitivity.