You are here

public function ConfigEntityTest::testCRUDUI in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/config/tests/src/Functional/ConfigEntityTest.php \Drupal\Tests\config\Functional\ConfigEntityTest::testCRUDUI()
  2. 10 core/modules/config/tests/src/Functional/ConfigEntityTest.php \Drupal\Tests\config\Functional\ConfigEntityTest::testCrudUi()

Tests CRUD operations through the UI.

File

core/modules/config/tests/src/Functional/ConfigEntityTest.php, line 228

Class

ConfigEntityTest
Tests configuration entities.

Namespace

Drupal\Tests\config\Functional

Code

public function testCRUDUI() {
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'administer site configuration',
  ]));
  $id = strtolower($this
    ->randomMachineName());
  $label1 = $this
    ->randomMachineName();
  $label2 = $this
    ->randomMachineName();
  $label3 = $this
    ->randomMachineName();
  $message_insert = new FormattableMarkup('%label configuration has been created.', [
    '%label' => $label1,
  ]);
  $message_update = new FormattableMarkup('%label configuration has been updated.', [
    '%label' => $label2,
  ]);
  $message_delete = new FormattableMarkup('The test configuration %label has been deleted.', [
    '%label' => $label2,
  ]);

  // Create a configuration entity.
  $edit = [
    'id' => $id,
    'label' => $label1,
  ];
  $this
    ->drupalPostForm('admin/structure/config_test/add', $edit, 'Save');
  $this
    ->assertUrl('admin/structure/config_test');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertRaw($message_insert);
  $this
    ->assertNoRaw($message_update);
  $this
    ->assertLinkByHref("admin/structure/config_test/manage/{$id}");

  // Update the configuration entity.
  $edit = [
    'label' => $label2,
  ];
  $this
    ->drupalPostForm("admin/structure/config_test/manage/{$id}", $edit, 'Save');
  $this
    ->assertUrl('admin/structure/config_test');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertNoRaw($message_insert);
  $this
    ->assertRaw($message_update);
  $this
    ->assertLinkByHref("admin/structure/config_test/manage/{$id}");
  $this
    ->assertLinkByHref("admin/structure/config_test/manage/{$id}/delete");

  // Delete the configuration entity.
  $this
    ->drupalGet("admin/structure/config_test/manage/{$id}");
  $this
    ->clickLink(t('Delete'));
  $this
    ->assertUrl("admin/structure/config_test/manage/{$id}/delete");
  $this
    ->drupalPostForm(NULL, [], 'Delete');
  $this
    ->assertUrl('admin/structure/config_test');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertNoRaw($message_update);
  $this
    ->assertRaw($message_delete);
  $this
    ->assertNoText($label1);
  $this
    ->assertNoLinkByHref("admin/structure/config_test/manage/{$id}");

  // Re-create a configuration entity.
  $edit = [
    'id' => $id,
    'label' => $label1,
  ];
  $this
    ->drupalPostForm('admin/structure/config_test/add', $edit, 'Save');
  $this
    ->assertUrl('admin/structure/config_test');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertText($label1);
  $this
    ->assertLinkByHref("admin/structure/config_test/manage/{$id}");

  // Rename the configuration entity's ID/machine name.
  $edit = [
    'id' => strtolower($this
      ->randomMachineName()),
    'label' => $label3,
  ];
  $this
    ->drupalPostForm("admin/structure/config_test/manage/{$id}", $edit, 'Save');
  $this
    ->assertUrl('admin/structure/config_test');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertNoText($label1);
  $this
    ->assertNoText($label2);
  $this
    ->assertText($label3);
  $this
    ->assertNoLinkByHref("admin/structure/config_test/manage/{$id}");
  $id = $edit['id'];
  $this
    ->assertLinkByHref("admin/structure/config_test/manage/{$id}");

  // Create a configuration entity with '0' machine name.
  $edit = [
    'id' => '0',
    'label' => '0',
  ];
  $this
    ->drupalPostForm('admin/structure/config_test/add', $edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $message_insert = new FormattableMarkup('%label configuration has been created.', [
    '%label' => $edit['label'],
  ]);
  $this
    ->assertRaw($message_insert);
  $this
    ->assertLinkByHref('admin/structure/config_test/manage/0');
  $this
    ->assertLinkByHref('admin/structure/config_test/manage/0/delete');
  $this
    ->drupalPostForm('admin/structure/config_test/manage/0/delete', [], 'Delete');
  $storage = \Drupal::entityTypeManager()
    ->getStorage('config_test');
  $this
    ->assertNull($storage
    ->load(0), 'Test entity deleted');

  // Create a configuration entity with a property that uses AJAX to show
  // extra form elements. Test this scenario in a non-JS case by using a
  // 'js-hidden' submit button.
  // @see \Drupal\Tests\config\FunctionalJavascript\ConfigEntityTest::testAjaxOnAddPage()
  $this
    ->drupalGet('admin/structure/config_test/add');
  $id = strtolower($this
    ->randomMachineName());
  $edit = [
    'id' => $id,
    'label' => $this
      ->randomString(),
    'size' => 'custom',
  ];
  $this
    ->assertFieldByName('size');
  $this
    ->assertNoFieldByName('size_value');
  $this
    ->drupalPostForm(NULL, $edit, 'Change size');
  $this
    ->assertFieldByName('size');
  $this
    ->assertFieldByName('size_value');

  // Submit the form with the regular 'Save' button and check that the entity
  // values are correct.
  $edit += [
    'size_value' => 'medium',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $entity = $storage
    ->load($id);
  $this
    ->assertEquals('custom', $entity
    ->get('size'));
  $this
    ->assertEquals('medium', $entity
    ->get('size_value'));
}