You are here

function ConfigEntityTest::testCRUDUI in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/config/src/Tests/ConfigEntityTest.php \Drupal\config\Tests\ConfigEntityTest::testCRUDUI()

Tests CRUD operations through the UI.

File

core/modules/config/src/Tests/ConfigEntityTest.php, line 237
Contains \Drupal\config\Tests\ConfigEntityTest.

Class

ConfigEntityTest
Tests configuration entities.

Namespace

Drupal\config\Tests

Code

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

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

  // Update the configuration entity.
  $edit = array(
    'label' => $label2,
  );
  $this
    ->drupalPostForm("admin/structure/config_test/manage/{$id}", $edit, 'Save');
  $this
    ->assertUrl('admin/structure/config_test');
  $this
    ->assertResponse(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, array(), 'Delete');
  $this
    ->assertUrl('admin/structure/config_test');
  $this
    ->assertResponse(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 = array(
    'id' => $id,
    'label' => $label1,
  );
  $this
    ->drupalPostForm('admin/structure/config_test/add', $edit, 'Save');
  $this
    ->assertUrl('admin/structure/config_test');
  $this
    ->assertResponse(200);
  $this
    ->assertText($label1);
  $this
    ->assertLinkByHref("admin/structure/config_test/manage/{$id}");

  // Rename the configuration entity's ID/machine name.
  $edit = array(
    'id' => strtolower($this
      ->randomMachineName()),
    'label' => $label3,
  );
  $this
    ->drupalPostForm("admin/structure/config_test/manage/{$id}", $edit, 'Save');
  $this
    ->assertUrl('admin/structure/config_test');
  $this
    ->assertResponse(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 = array(
    'id' => '0',
    'label' => '0',
  );
  $this
    ->drupalPostForm('admin/structure/config_test/add', $edit, 'Save');
  $this
    ->assertResponse(200);
  $message_insert = format_string('%label configuration has been created.', array(
    '%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', array(), 'Delete');
  $this
    ->assertFalse(entity_load('config_test', '0'), 'Test entity deleted');

  // Create a configuration entity with a property that uses AJAX to show
  // extra form elements.
  $this
    ->drupalGet('admin/structure/config_test/add');

  // Test that the dependent element is not shown initially.
  $this
    ->assertFieldByName('size');
  $this
    ->assertNoFieldByName('size_value');
  $id = strtolower($this
    ->randomMachineName());
  $edit = [
    'id' => $id,
    'label' => $this
      ->randomString(),
    'size' => 'custom',
  ];
  $this
    ->drupalPostAjaxForm(NULL, $edit, 'size');

  // Check that the dependent element is shown after selecting a 'size' value.
  $this
    ->assertFieldByName('size');
  $this
    ->assertFieldByName('size_value');

  // Test the same scenario but it in a non-JS case by using a 'js-hidden'
  // submit button.
  $this
    ->drupalGet('admin/structure/config_test/add');
  $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 = entity_load('config_test', $id);
  $this
    ->assertEqual($entity
    ->get('size'), 'custom');
  $this
    ->assertEqual($entity
    ->get('size_value'), 'medium');
}