entity_example.test 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @file
  4. * Tests for entity_example module.
  5. *
  6. * Verify example module functionality.
  7. */
  8. /**
  9. * Functionality tests for entity example module.
  10. *
  11. * @ingroup entity_example
  12. */
  13. class EntityExampleTestCase extends DrupalWebTestCase {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public static function getInfo() {
  18. return array(
  19. 'name' => 'Entity example',
  20. 'description' => 'Basic entity example tests',
  21. 'group' => 'Examples',
  22. );
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function setUp() {
  28. // Enable the module.
  29. parent::setUp('entity_example');
  30. // Create and login user with access.
  31. $permissions = array(
  32. 'access content',
  33. 'view any entity_example_basic entity',
  34. 'edit any entity_example_basic entity',
  35. 'create entity_example_basic entities',
  36. 'administer entity_example_basic entities',
  37. 'administer site configuration',
  38. 'administer fields',
  39. );
  40. $account = $this->drupalCreateUser($permissions);
  41. $this->drupalLogin($account);
  42. // Attach a field.
  43. $field = array(
  44. 'field_name' => 'entity_example_test_text' ,
  45. 'type' => 'text',
  46. );
  47. field_create_field($field);
  48. $instance = array(
  49. 'label' => 'Subject',
  50. 'field_name' => 'entity_example_test_text',
  51. 'entity_type' => 'entity_example_basic',
  52. 'bundle' => 'first_example_bundle',
  53. );
  54. field_create_instance($instance);
  55. }
  56. /**
  57. * Test Entity Example features.
  58. *
  59. * - CRUD
  60. * - Table display
  61. * - User access
  62. * - Field management
  63. * - Display management
  64. */
  65. public function testEntityExampleBasic() {
  66. // Create 10 entities.
  67. for ($i = 1; $i <= 10; $i++) {
  68. $edit[$i]['item_description'] = $this->randomName();
  69. $edit[$i]['entity_example_test_text[und][0][value]'] = $this->randomName(32);
  70. $this->drupalPost('examples/entity_example/basic/add', $edit[$i], 'Save');
  71. $this->assertText('item_description=' . $edit[$i]['item_description']);
  72. $this->drupalGet('examples/entity_example/basic/' . $i);
  73. $this->assertText('item_description=' . $edit[$i]['item_description']);
  74. $this->assertText($edit[$i]['entity_example_test_text[und][0][value]']);
  75. }
  76. // Delete entity 5.
  77. $this->drupalPost('examples/entity_example/basic/5/edit', $edit[5], 'Delete');
  78. $this->drupalGet('examples/entity_example/basic/5');
  79. $this->assertResponse(404, 'Deleted entity 5 no longer exists');
  80. unset($edit[5]);
  81. // Update entity 2 and verify the update.
  82. $edit[2] = array(
  83. 'item_description' => 'updated entity 2 ',
  84. 'entity_example_test_text[und][0][value]' => 'updated entity 2 test text',
  85. );
  86. $this->drupalPost('examples/entity_example/basic/2/edit', $edit[2], 'Save');
  87. $this->assertText('item_description=' . $edit[2]['item_description']);
  88. $this->assertText('updated entity 2 test text');
  89. // View the entity list page and verify that the items which still exist
  90. // are there, and that the deleted #5 no longer is there.
  91. $this->drupalGet('admin/structure/entity_example_basic/manage');
  92. foreach ($edit as $id => $item) {
  93. $this->assertRaw('examples/entity_example/basic/' . $id . '">' . $item['item_description'] . '</a>');
  94. }
  95. $this->assertNoRaw('examples/entity_example/basic/5">');
  96. // Add a field through the field UI and verify that it behaves correctly.
  97. $field_edit = array(
  98. 'fields[_add_new_field][label]' => 'New junk field',
  99. 'fields[_add_new_field][field_name]' => 'new_junk_field',
  100. 'fields[_add_new_field][type]' => 'text',
  101. 'fields[_add_new_field][widget_type]' => 'text_textfield',
  102. );
  103. $this->drupalPost('admin/structure/entity_example_basic/manage/fields', $field_edit, t('Save'));
  104. $this->drupalPost(NULL, array(), t('Save field settings'));
  105. $this->drupalPost(NULL, array(), t('Save settings'));
  106. $this->assertResponse(200);
  107. // Now verify that we can edit and view this entity with fields.
  108. $edit[10]['field_new_junk_field[und][0][value]'] = $this->randomName();
  109. $this->drupalPost('examples/entity_example/basic/10/edit', $edit[10], 'Save');
  110. $this->assertResponse(200);
  111. $this->assertText('item_description=' . $edit[10]['item_description']);
  112. $this->assertText($edit[10]['field_new_junk_field[und][0][value]'], 'Custom field updated successfully');
  113. // Create and login user without view access.
  114. $account = $this->drupalCreateUser(array('access content'));
  115. $this->drupalLogin($account);
  116. $this->drupalGet('admin/structure/entity_example_basic/manage');
  117. $this->assertResponse(403);
  118. $this->drupalGet('examples/entity_example/basic/2');
  119. $this->assertResponse(403, 'User does not have permission to view entity');
  120. // Create and login user with view access but no edit access.
  121. $account = $this->drupalCreateUser(array('access content', 'view any entity_example_basic entity'));
  122. $this->drupalLogin($account);
  123. $this->drupalGet('admin/structure/entity_example_basic/manage');
  124. $this->assertResponse(403, 'Denied access to admin manage page');
  125. $this->drupalGet('examples/entity_example/basic/2');
  126. $this->assertResponse(200, 'User has permission to view entity');
  127. $this->drupalGet('examples/entity_example/basic/2/edit');
  128. $this->assertResponse(403, 'User is denied edit privileges');
  129. // Create and login user with view and edit but no manage privs.
  130. $account = $this->drupalCreateUser(
  131. array(
  132. 'access content',
  133. 'view any entity_example_basic entity',
  134. 'edit any entity_example_basic entity',
  135. )
  136. );
  137. $this->drupalLogin($account);
  138. $this->drupalGet('admin/structure/entity_example_basic/manage');
  139. $this->assertResponse(403, 'Denied access to admin manage page');
  140. $this->drupalGet('examples/entity_example/basic/2');
  141. $this->assertResponse(200, 'User has permission to view entity');
  142. $this->drupalGet('examples/entity_example/basic/2/edit');
  143. $this->assertResponse(200, 'User has edit privileges');
  144. }
  145. }