function ConfigEntityTest::testCrudUi

Same name and namespace in other branches
  1. 9 core/modules/config/tests/src/Functional/ConfigEntityTest.php \Drupal\Tests\config\Functional\ConfigEntityTest::testCrudUi()
  2. 8.9.x core/modules/config/tests/src/Functional/ConfigEntityTest.php \Drupal\Tests\config\Functional\ConfigEntityTest::testCRUDUI()
  3. 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 230

Class

ConfigEntityTest
Tests configuration entities.

Namespace

Drupal\Tests\config\Functional

Code

public function testCrudUi() : void {
    $this->drupalLogin($this->drupalCreateUser([
        'administer site configuration',
    ]));
    $id = $this->randomMachineName();
    $label1 = $this->randomMachineName();
    $label2 = $this->randomMachineName();
    $label3 = $this->randomMachineName();
    $message_insert = "{$label1} configuration has been created.";
    $message_update = "{$label2} configuration has been updated.";
    $message_delete = "The test configuration {$label2} has been deleted.";
    // Create a configuration entity.
    $edit = [
        'id' => $id,
        'label' => $label1,
    ];
    $this->drupalGet('admin/structure/config_test/add');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->addressEquals('admin/structure/config_test');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains($message_insert);
    $this->assertSession()
        ->pageTextNotContains($message_update);
    $this->assertSession()
        ->linkByHrefExists("admin/structure/config_test/manage/{$id}");
    // Update the configuration entity.
    $edit = [
        'label' => $label2,
    ];
    $this->drupalGet("admin/structure/config_test/manage/{$id}");
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->addressEquals('admin/structure/config_test');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextNotContains($message_insert);
    $this->assertSession()
        ->pageTextContains($message_update);
    $this->assertSession()
        ->linkByHrefExists("admin/structure/config_test/manage/{$id}");
    $this->assertSession()
        ->linkByHrefExists("admin/structure/config_test/manage/{$id}/delete");
    // Delete the configuration entity.
    $this->drupalGet("admin/structure/config_test/manage/{$id}");
    $this->clickLink('Delete');
    $this->assertSession()
        ->addressEquals("admin/structure/config_test/manage/{$id}/delete");
    $this->submitForm([], 'Delete');
    $this->assertSession()
        ->addressEquals('admin/structure/config_test');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextNotContains($message_update);
    $this->assertSession()
        ->pageTextContains($message_delete);
    $this->assertSession()
        ->pageTextNotContains($label1);
    $this->assertSession()
        ->linkByHrefNotExists("admin/structure/config_test/manage/{$id}");
    // Re-create a configuration entity.
    $edit = [
        'id' => $id,
        'label' => $label1,
    ];
    $this->drupalGet('admin/structure/config_test/add');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->addressEquals('admin/structure/config_test');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains($label1);
    $this->assertSession()
        ->linkByHrefExists("admin/structure/config_test/manage/{$id}");
    // Rename the configuration entity's ID/machine name.
    $edit = [
        'id' => $this->randomMachineName(),
        'label' => $label3,
    ];
    $this->drupalGet("admin/structure/config_test/manage/{$id}");
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->addressEquals('admin/structure/config_test');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextNotContains($label1);
    $this->assertSession()
        ->pageTextNotContains($label2);
    $this->assertSession()
        ->pageTextContains($label3);
    $this->assertSession()
        ->linkByHrefNotExists("admin/structure/config_test/manage/{$id}");
    $id = $edit['id'];
    $this->assertSession()
        ->linkByHrefExists("admin/structure/config_test/manage/{$id}");
    // Create a configuration entity with '0' machine name.
    $edit = [
        'id' => '0',
        'label' => '0',
    ];
    $this->drupalGet('admin/structure/config_test/add');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('0 configuration has been created.');
    $this->assertSession()
        ->linkByHrefExists('admin/structure/config_test/manage/0');
    $this->assertSession()
        ->linkByHrefExists('admin/structure/config_test/manage/0/delete');
    $this->drupalGet('admin/structure/config_test/manage/0/delete');
    $this->submitForm([], '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 = $this->randomMachineName();
    $edit = [
        'id' => $id,
        'label' => $this->randomString(),
        'size' => 'custom',
    ];
    $this->assertSession()
        ->fieldExists('size');
    $this->assertSession()
        ->fieldNotExists('size_value');
    $this->submitForm($edit, 'Change size');
    $this->assertSession()
        ->fieldExists('size');
    $this->assertSession()
        ->fieldExists('size_value');
    // Submit the form with the regular 'Save' button and check that the entity
    // values are correct.
    $edit += [
        'size_value' => 'medium',
    ];
    $this->submitForm($edit, 'Save');
    $entity = $storage->load($id);
    $this->assertEquals('custom', $entity->get('size'));
    $this->assertEquals('medium', $entity->get('size_value'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.