3 namespace Drupal\Tests\system\Functional\Entity;
5 use Drupal\entity_test\Entity\EntityTestBundle;
6 use Drupal\entity_test\Entity\EntityTestMul;
7 use Drupal\entity_test\Entity\EntityTestWithBundle;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests the /add and /add/{type} controllers.
15 class EntityAddUITest extends BrowserTestBase {
20 public static $modules = ['entity_test'];
23 * Tests the add page for an entity type using bundle entities.
25 public function testAddPageWithBundleEntities() {
26 $admin_user = $this->drupalCreateUser([
27 'administer entity_test_with_bundle content',
29 $this->drupalLogin($admin_user);
31 // Users without create access for bundles do not have access to the add
32 // page if there are no bundles.
33 $this->drupalGet('/entity_test_with_bundle/add');
34 $this->assertResponse(403);
36 $bundle_admin_user = $this->drupalCreateUser([
37 'administer entity_test_with_bundle content',
38 'administer entity_test_bundle content',
40 $this->drupalLogin($bundle_admin_user);
42 // No bundles exist, the add bundle message should be present as the user
43 // has the necessary permissions.
44 $this->drupalGet('/entity_test_with_bundle/add');
45 $this->assertText('There is no test entity bundle yet.');
46 $this->assertLink('Add a new test entity bundle.');
48 // One bundle exists, confirm redirection to the add-form.
49 EntityTestBundle::create([
51 'label' => 'Test label',
52 'description' => 'My test description',
54 $this->drupalGet('/entity_test_with_bundle/add');
55 $this->assertUrl('/entity_test_with_bundle/add/test');
57 // Two bundles exist, confirm both are shown.
58 EntityTestBundle::create([
60 'label' => 'Test2 label',
61 'description' => 'My test2 description',
63 $this->drupalGet('/entity_test_with_bundle/add');
65 $this->assertLink('Test label');
66 $this->assertLink('Test2 label');
67 $this->assertText('My test description');
68 $this->assertText('My test2 description');
70 $this->clickLink('Test2 label');
71 $this->drupalGet('/entity_test_with_bundle/add/test2');
73 $this->drupalPostForm(NULL, ['name[0][value]' => 'test name'], t('Save'));
74 $entity = EntityTestWithBundle::load(1);
75 $this->assertEqual('test name', $entity->label());
77 // Create a new user that only has bundle specific permissions.
78 $user = $this->drupalCreateUser([
79 'create test entity_test_with_bundle entities',
80 'create test2 entity_test_with_bundle entities',
82 $this->drupalLogin($user);
84 // Create a bundle that the user does not have create permissions for.
85 EntityTestBundle::create([
87 'label' => 'Test3 label',
88 'description' => 'My test3 description',
90 $this->drupalGet('/entity_test_with_bundle/add');
91 $this->assertLink('Test label');
92 $this->assertLink('Test2 label');
93 $this->assertNoLink('Test3 label');
94 $this->clickLink(t('Test label'));
95 $this->assertResponse(200);
97 // Without any permissions, access must be denied.
98 $this->drupalLogout();
99 $this->drupalGet('/entity_test_with_bundle/add');
100 $this->assertResponse(403);
102 // Create a new user that has bundle create permissions.
103 $user = $this->drupalCreateUser([
104 'administer entity_test_bundle content',
106 $this->drupalLogin($user);
108 // User has access to the add page but no bundles are shown because the user
109 // does not have bundle specific permissions. The add bundle message is
110 // present as the user has bundle create permissions.
111 $this->drupalGet('/entity_test_with_bundle/add');
112 $this->assertNoLink('Test label');
113 $this->assertNoLink('Test2 label');
114 $this->assertNoLink('Test3 label');
115 $this->assertLink('Add a new test entity bundle.');
119 * Tests the add page for an entity type not using bundle entities.
121 public function testAddPageWithoutBundleEntities() {
122 $admin_user = $this->drupalCreateUser([
123 'administer entity_test content',
125 $this->drupalLogin($admin_user);
127 entity_test_create_bundle('test', 'Test label', 'entity_test_mul');
128 // Delete the default bundle, so that we can rely on our own.
129 entity_test_delete_bundle('entity_test_mul', 'entity_test_mul');
131 // One bundle exists, confirm redirection to the add-form.
132 $this->drupalGet('/entity_test_mul/add');
133 $this->assertUrl('/entity_test_mul/add/test');
135 // Two bundles exist, confirm both are shown.
136 entity_test_create_bundle('test2', 'Test2 label', 'entity_test_mul');
137 $this->drupalGet('/entity_test_mul/add');
139 $this->assertLink('Test label');
140 $this->assertLink('Test2 label');
142 $this->clickLink('Test2 label');
143 $this->drupalGet('/entity_test_mul/add/test2');
145 $this->drupalPostForm(NULL, ['name[0][value]' => 'test name'], t('Save'));
146 $entity = EntityTestMul::load(1);
147 $this->assertEqual('test name', $entity->label());