You are here

public function DevelStateEditorTest::testStateEdit in Devel 8

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/DevelStateEditorTest.php \Drupal\Tests\devel\Functional\DevelStateEditorTest::testStateEdit()
  2. 8.2 tests/src/Functional/DevelStateEditorTest.php \Drupal\Tests\devel\Functional\DevelStateEditorTest::testStateEdit()
  3. 4.x tests/src/Functional/DevelStateEditorTest.php \Drupal\Tests\devel\Functional\DevelStateEditorTest::testStateEdit()

Tests state edit.

File

tests/src/Functional/DevelStateEditorTest.php, line 126

Class

DevelStateEditorTest
Tests devel state editor.

Namespace

Drupal\Tests\devel\Functional

Code

public function testStateEdit() {

  // Create some state variables for the test.
  $this->state
    ->set('devel.simple', 0);
  $this->state
    ->set('devel.array', [
    'devel' => 'value',
  ]);
  $this->state
    ->set('devel.object', $this
    ->randomObject());

  // Ensure that state edit form is accessible only by users with the
  // adequate permissions.
  $this
    ->drupalLogin($this->develUser);
  $this
    ->drupalGet('devel/state/edit/devel.simple');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->drupalLogin($this->adminUser);

  // Ensure that accessing an un-existent state variable cause a warning
  // message.
  $this
    ->drupalGet('devel/state/edit/devel.unknown');
  $this
    ->assertSession()
    ->pageTextContains(strtr('State @name does not exist in the system.', [
    '@name' => 'devel.unknown',
  ]));

  // Ensure that state variables that contain simple type can be edited and
  // saved.
  $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'));

  // Ensure that state variables that contain array can be edited and saved
  // and the new value is properly validated.
  $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'));

  // Try to save an invalid yaml input.
  $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'));

  // Ensure that state variables that contain objects cannot be edited.
  $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'));

  // Ensure that the cancel link works as expected.
  $this
    ->clickLink('Cancel');
  $this
    ->assertSession()
    ->addressEquals('devel/state');
}