View source
<?php
namespace Drupal\Tests\config_entity_example\Functional;
use Drupal\config_entity_example\Entity\Robot;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class ConfigEntityExampleTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'config_entity_example',
];
protected $profile = 'minimal';
public function testConfigEntityExample() {
$assert = $this
->assertSession();
$entity = Robot::load('marvin');
$this
->assertNotNull($entity, 'Marvin was created during installation.');
$forbidden_paths = [
'/examples/config-entity-example',
'/examples/config-entity-example/add',
'/examples/config-entity-example/manage/marvin',
'/examples/config-entity-example/manage/marvin/delete',
];
foreach ($forbidden_paths as $path) {
$this
->drupalGet($path);
$assert
->statusCodeEquals(403);
}
$noperms_user = $this
->drupalCreateUser();
$this
->drupalLogin($noperms_user);
foreach ($forbidden_paths as $path) {
$this
->drupalGet($path);
$assert
->statusCodeEquals(403);
}
$admin_user = $this
->drupalCreateUser([
'administer robots',
]);
$this
->drupalLogin($admin_user);
foreach ($forbidden_paths as $unforbidden) {
$this
->drupalGet($unforbidden);
$assert
->statusCodeEquals(200);
}
$this
->drupalGet('');
$assert
->linkByHrefExists('examples/config-entity-example');
$this
->drupalGet('/examples/config-entity-example');
$this
->clickLink('Add robot');
$robot_machine_name = 'roboname';
$this
->drupalPostForm(NULL, [
'label' => $robot_machine_name,
'id' => $robot_machine_name,
'floopy' => TRUE,
], 'Create Robot');
$this
->drupalGet('/examples/config-entity-example/manage/' . $robot_machine_name);
$assert
->fieldExists('label');
$assert
->checkboxChecked('edit-floopy');
$this
->drupalGet('/examples/config-entity-example');
$this
->clickLink('Add robot');
$robby_machine_name = 'robby_machine_name';
$robby_label = 'Robby label';
$this
->drupalPostForm(NULL, [
'label' => $robby_label,
'id' => $robby_machine_name,
'floopy' => TRUE,
], 'Create Robot');
$this
->drupalGet('/examples/config-entity-example');
$assert
->pageTextContains($robby_label);
$assert
->pageTextContains($robby_machine_name);
$this
->drupalPostForm(Url::fromRoute('entity.robot.add_form'), [
'label' => $robby_label,
'id' => $robby_machine_name,
'floopy' => TRUE,
], 'Create Robot');
$assert
->pageTextContains('The machine-readable name is already in use.');
$this
->drupalGet(Url::fromRoute('entity.robot.list'));
$this
->assertLinkByHref('/examples/config-entity-example/add');
$this
->assertLinkByHref('/examples/config-entity-example/manage/robby_machine_name');
$this
->assertLinkByHref('/examples/config-entity-example/manage/robby_machine_name/delete');
$this
->drupalGet('/examples/config-entity-example/add');
$this
->assertActionButton('examples/config-entity-example');
$this
->drupalGet('/examples/config-entity-example/manage/robby_machine_name');
$this
->assertLinkByHref('/examples/config-entity-example/manage/robby_machine_name/delete');
$this
->assertActionButton('examples/config-entity-example');
$this
->drupalGet('/examples/config-entity-example/manage/robby_machine_name/delete');
$cancel_button = $this
->xpath('//a[@id="edit-cancel" and contains(@href, :path)]', [
':path' => '/examples/config-entity-example',
]);
$this
->assertEqual(count($cancel_button), 1, 'Found cancel button linking to list page.');
$this
->drupalPostForm(Url::fromRoute('entity.robot.add_form'), [
'label' => 'Custom',
'id' => 'custom',
'floopy' => TRUE,
], 'Create Robot');
$assert
->pageTextContains('Additionally, it can not be the reserved word "custom".');
}
protected function assertActionButton($path) {
$button_element = $this
->xpath('//a[contains(@class, "button-action") and contains(@data-drupal-link-system-path, :path)]', [
':path' => $path,
]);
$this
->assertEqual(count($button_element), 1, 'Found action button for path: ' . $path);
}
}