View source
<?php
namespace Drupal\Tests\riddler\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class RiddlerTestCase extends BrowserTestBase {
use UserCreationTrait;
const RIDDLER_ADMIN_PATH = 'admin/config/people/captcha/riddler';
public static $modules = [
'block',
'comment',
'captcha',
'riddler',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer CAPTCHA settings',
]);
$this
->drupalLogin($this->adminUser);
}
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';
$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');
$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');
$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');
$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');
}
}