|
4 | 4 |
|
5 | 5 | use Drupal\content_entity_example\Entity\Contact;
|
6 | 6 | use Drupal\Tests\examples\Functional\ExamplesBrowserTestBase;
|
| 7 | +use Drupal\Core\Url; |
7 | 8 |
|
8 | 9 | /**
|
9 | 10 | * Tests the basic functions of the Content Entity Example module.
|
@@ -244,4 +245,63 @@ public function testAddFields() {
|
244 | 245 | $this->assertEquals($expected_path, $current_path);
|
245 | 246 | }
|
246 | 247 |
|
| 248 | + /** |
| 249 | + * Ensure admin and permissioned users can create contacts. |
| 250 | + */ |
| 251 | + public function testCreateAdminPermission() { |
| 252 | + $assert = $this->assertSession(); |
| 253 | + $add_url = Url::fromRoute('content_entity_example.contact_add'); |
| 254 | + |
| 255 | + // Create a Contact entity object so that we can query it for it's annotated |
| 256 | + // properties. We don't need to save it. |
| 257 | + /* @var $contact \Drupal\content_entity_example\Entity\Contact */ |
| 258 | + $contact = Contact::create(); |
| 259 | + |
| 260 | + // Create an admin user and log them in. We use the entity annotation for |
| 261 | + // admin_permission in order to validate it. We also have to add the view |
| 262 | + // list permission because the add form redirects to the list on success. |
| 263 | + $this->drupalLogin($this->drupalCreateUser([ |
| 264 | + $contact->getEntityType()->getAdminPermission(), |
| 265 | + 'view contact entity', |
| 266 | + ])); |
| 267 | + |
| 268 | + // Post a contact. |
| 269 | + $edit = [ |
| 270 | + 'name[0][value]' => 'Test Admin Name', |
| 271 | + 'first_name[0][value]' => 'Admin First Name', |
| 272 | + 'gender' => 'female', |
| 273 | + 'role' => 'administrator', |
| 274 | + ]; |
| 275 | + $this->drupalPostForm($add_url, $edit, 'Save'); |
| 276 | + $assert->statusCodeEquals(200); |
| 277 | + $assert->pageTextContains('Test Admin Name'); |
| 278 | + |
| 279 | + // Create a user with 'add contact entity' permission. We also have to add |
| 280 | + // the view list permission because the add form redirects to the list on |
| 281 | + // success. |
| 282 | + $this->drupalLogin($this->drupalCreateUser([ |
| 283 | + 'add contact entity', |
| 284 | + 'view contact entity', |
| 285 | + ])); |
| 286 | + |
| 287 | + // Post a contact. |
| 288 | + $edit = [ |
| 289 | + 'name[0][value]' => 'Mere Mortal Name', |
| 290 | + 'first_name[0][value]' => 'Mortal First Name', |
| 291 | + 'gender' => 'male', |
| 292 | + 'role' => 'user', |
| 293 | + ]; |
| 294 | + $this->drupalPostForm($add_url, $edit, 'Save'); |
| 295 | + $assert->statusCodeEquals(200); |
| 296 | + $assert->pageTextContains('Mere Mortal Name'); |
| 297 | + |
| 298 | + // Finally, a user who can only view should not be able to get to the add |
| 299 | + // form. |
| 300 | + $this->drupalLogin($this->drupalCreateUser([ |
| 301 | + 'view contact entity', |
| 302 | + ])); |
| 303 | + $this->drupalGet($add_url); |
| 304 | + $assert->statusCodeEquals(403); |
| 305 | + } |
| 306 | + |
247 | 307 | }
|
0 commit comments