View source
<?php
namespace Drupal\Tests\devel\Functional;
use Behat\Mink\Element\NodeElement;
class DevelStateEditorTest extends DevelBrowserTestBase {
protected $state;
public function setUp() {
parent::setUp();
$this->state = $this->container
->get('state');
$this
->drupalPlaceBlock('page_title_block');
}
public function testStateEditMenuLink() {
$this
->drupalPlaceBlock('system_menu_block:devel');
$this
->drupalLogin($this->develUser);
$this
->drupalGet('');
$this
->clickLink('State editor');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals('/devel/state');
$this
->assertSession()
->pageTextContains('State editor');
}
public function testStateListing() {
$table_selector = 'table.devel-state-list';
$this
->drupalGet('devel/state');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->develUser);
$this
->drupalGet('devel/state');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('State editor');
$table = $this
->assertSession()
->elementExists('css', $table_selector);
$states = \Drupal::keyValue('state')
->getAll();
$rows = $table
->findAll('css', 'tbody tr');
$this
->assertEquals(count($rows), count($states), 'All states are listed in the table.');
$this->state
->set('devel.simple', 'Hello!');
$this
->drupalGet('devel/state');
$table = $this
->assertSession()
->elementExists('css', $table_selector);
$this
->assertSession()
->elementExists('css', sprintf('tbody td:contains("%s")', 'devel.simple'), $table);
$headers = $table
->findAll('css', 'thead th');
$this
->assertEquals(count($headers), 2, 'Correct number of table header cells found.');
$this
->assertElementsTextEquals($headers, [
'Name',
'Value',
]);
$this
->assertSession()
->elementNotExists('css', 'ul.dropbutton li a', $table);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('devel/state');
$table = $this
->assertSession()
->elementExists('css', $table_selector);
$headers = $table
->findAll('css', 'thead th');
$this
->assertEquals(count($headers), 3, 'Correct number of table header cells found.');
$this
->assertElementsTextEquals($headers, [
'Name',
'Value',
'Operations',
]);
$this
->assertSession()
->elementExists('css', 'ul.dropbutton li a', $table);
$this
->clickLink('Edit');
$this
->assertSession()
->statusCodeEquals(200);
}
public function testStateEdit() {
$this->state
->set('devel.simple', 0);
$this->state
->set('devel.array', [
'devel' => 'value',
]);
$this->state
->set('devel.object', $this
->randomObject());
$this
->drupalLogin($this->develUser);
$this
->drupalGet('devel/state/edit/devel.simple');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('devel/state/edit/devel.unknown');
$this
->assertSession()
->pageTextContains(strtr('State @name does not exist in the system.', [
'@name' => 'devel.unknown',
]));
$this
->drupalGet('devel/state/edit/devel.simple');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains(strtr('Edit state variable: @name', [
'@name' => 'devel.simple',
]));
$input = $this
->assertSession()
->fieldExists('edit-new-value');
$this
->assertFalse($input
->hasAttribute('disabled'));
$button = $this
->assertSession()
->buttonExists('edit-submit');
$this
->assertFalse($button
->hasAttribute('disabled'));
$edit = [
'new_value' => 1,
];
$this
->drupalPostForm('devel/state/edit/devel.simple', $edit, 'Save');
$this
->assertSession()
->pageTextContains(strtr('Variable @name was successfully edited.', [
'@name' => 'devel.simple',
]));
$this
->assertEquals(1, $this->state
->get('devel.simple'));
$this
->drupalGet('devel/state/edit/devel.array');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains(strtr('Edit state variable: @name', [
'@name' => 'devel.array',
]));
$input = $this
->assertSession()
->fieldExists('edit-new-value');
$this
->assertFalse($input
->hasAttribute('disabled'));
$button = $this
->assertSession()
->buttonExists('edit-submit');
$this
->assertFalse($button
->hasAttribute('disabled'));
$edit = [
'new_value' => 'devel: \'value updated',
];
$this
->drupalPostForm('devel/state/edit/devel.array', $edit, 'Save');
$this
->assertSession()
->pageTextContains('Invalid input:');
$edit = [
'new_value' => 'devel: \'value updated\'',
];
$this
->drupalPostForm('devel/state/edit/devel.array', $edit, 'Save');
$this
->assertSession()
->pageTextContains(strtr('Variable @name was successfully edited.', [
'@name' => 'devel.array',
]));
$this
->assertEquals([
'devel' => 'value updated',
], $this->state
->get('devel.array'));
$this
->drupalGet('devel/state/edit/devel.object');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains(strtr('Edit state variable: @name', [
'@name' => 'devel.object',
]));
$this
->assertSession()
->pageTextContains(strtr('Only simple structures are allowed to be edited. State @name contains objects.', [
'@name' => 'devel.object',
]));
$this
->assertSession()
->fieldDisabled('edit-new-value');
$button = $this
->assertSession()
->buttonExists('edit-submit');
$this
->assertTrue($button
->hasAttribute('disabled'));
$this
->clickLink('Cancel');
$this
->assertSession()
->addressEquals('devel/state');
}
protected function assertElementsTextEquals(array $elements, array $expected_elements_text) {
$actual_text = array_map(function (NodeElement $element) {
return $element
->getText();
}, $elements);
$this
->assertSame($expected_elements_text, $actual_text);
}
}