View source
<?php
namespace Drupal\Tests\system\Functional\Entity;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\entity_test\Entity\EntityTestWithBundle;
use Drupal\Tests\BrowserTestBase;
class EntityAddUITest extends BrowserTestBase {
protected static $modules = [
'entity_test',
];
protected $defaultTheme = 'stark';
public function testAddPageWithBundleEntities() {
$admin_user = $this
->drupalCreateUser([
'administer entity_test_with_bundle content',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('/entity_test_with_bundle/add');
$this
->assertSession()
->statusCodeEquals(403);
$bundle_admin_user = $this
->drupalCreateUser([
'administer entity_test_with_bundle content',
'administer entity_test_bundle content',
]);
$this
->drupalLogin($bundle_admin_user);
$this
->drupalGet('/entity_test_with_bundle/add');
$this
->assertSession()
->pageTextContains('There is no test entity bundle yet.');
$this
->assertSession()
->linkExists('Add a new test entity bundle.');
EntityTestBundle::create([
'id' => 'test',
'label' => 'Test label',
'description' => 'My test description',
])
->save();
$this
->drupalGet('/entity_test_with_bundle/add');
$this
->assertSession()
->addressEquals('/entity_test_with_bundle/add/test');
EntityTestBundle::create([
'id' => 'test2',
'label' => 'Test2 label',
'description' => 'My test2 description',
])
->save();
$this
->drupalGet('/entity_test_with_bundle/add');
$this
->assertSession()
->linkExists('Test label');
$this
->assertSession()
->linkExists('Test2 label');
$this
->assertSession()
->pageTextContains('My test description');
$this
->assertSession()
->pageTextContains('My test2 description');
$this
->clickLink('Test2 label');
$this
->drupalGet('/entity_test_with_bundle/add/test2');
$this
->submitForm([
'name[0][value]' => 'test name',
], 'Save');
$entity = EntityTestWithBundle::load(1);
$this
->assertEquals('test name', $entity
->label());
$user = $this
->drupalCreateUser([
'create test entity_test_with_bundle entities',
'create test2 entity_test_with_bundle entities',
]);
$this
->drupalLogin($user);
EntityTestBundle::create([
'id' => 'test3',
'label' => 'Test3 label',
'description' => 'My test3 description',
])
->save();
$this
->drupalGet('/entity_test_with_bundle/add');
$this
->assertSession()
->linkExists('Test label');
$this
->assertSession()
->linkExists('Test2 label');
$this
->assertSession()
->linkNotExists('Test3 label');
$this
->clickLink('Test label');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogout();
$this
->drupalGet('/entity_test_with_bundle/add');
$this
->assertSession()
->statusCodeEquals(403);
$user = $this
->drupalCreateUser([
'administer entity_test_bundle content',
]);
$this
->drupalLogin($user);
$this
->drupalGet('/entity_test_with_bundle/add');
$this
->assertSession()
->linkNotExists('Test label');
$this
->assertSession()
->linkNotExists('Test2 label');
$this
->assertSession()
->linkNotExists('Test3 label');
$this
->assertSession()
->linkExists('Add a new test entity bundle.');
}
public function testAddPageWithoutBundleEntities() {
$admin_user = $this
->drupalCreateUser([
'administer entity_test content',
]);
$this
->drupalLogin($admin_user);
entity_test_create_bundle('test', 'Test label', 'entity_test_mul');
entity_test_delete_bundle('entity_test_mul', 'entity_test_mul');
$this
->drupalGet('/entity_test_mul/add');
$this
->assertSession()
->addressEquals('/entity_test_mul/add/test');
entity_test_create_bundle('test2', 'Test2 label', 'entity_test_mul');
$this
->drupalGet('/entity_test_mul/add');
$this
->assertSession()
->linkExists('Test label');
$this
->assertSession()
->linkExists('Test2 label');
$this
->clickLink('Test2 label');
$this
->drupalGet('/entity_test_mul/add/test2');
$this
->submitForm([
'name[0][value]' => 'test name',
], 'Save');
$entity = EntityTestMul::load(1);
$this
->assertEquals('test name', $entity
->label());
}
}