NodeTypeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace Drupal\node\Tests;
  3. use Drupal\field\Entity\FieldConfig;
  4. use Drupal\node\Entity\NodeType;
  5. use Drupal\Core\Url;
  6. use Drupal\system\Tests\Menu\AssertBreadcrumbTrait;
  7. /**
  8. * Ensures that node type functions work correctly.
  9. *
  10. * @group node
  11. */
  12. class NodeTypeTest extends NodeTestBase {
  13. use AssertBreadcrumbTrait;
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = ['field_ui', 'block'];
  20. /**
  21. * Ensures that node type functions (node_type_get_*) work correctly.
  22. *
  23. * Load available node types and validate the returned data.
  24. */
  25. public function testNodeTypeGetFunctions() {
  26. $node_types = NodeType::loadMultiple();
  27. $node_names = node_type_get_names();
  28. $this->assertTrue(isset($node_types['article']), 'Node type article is available.');
  29. $this->assertTrue(isset($node_types['page']), 'Node type basic page is available.');
  30. $this->assertEqual($node_types['article']->label(), $node_names['article'], 'Correct node type base has been returned.');
  31. $article = NodeType::load('article');
  32. $this->assertEqual($node_types['article'], $article, 'Correct node type has been returned.');
  33. $this->assertEqual($node_types['article']->label(), $article->label(), 'Correct node type name has been returned.');
  34. }
  35. /**
  36. * Tests creating a content type programmatically and via a form.
  37. */
  38. public function testNodeTypeCreation() {
  39. // Create a content type programmatically.
  40. $type = $this->drupalCreateContentType();
  41. $type_exists = (bool) NodeType::load($type->id());
  42. $this->assertTrue($type_exists, 'The new content type has been created in the database.');
  43. // Log in a test user.
  44. $web_user = $this->drupalCreateUser(['create ' . $type->label() . ' content']);
  45. $this->drupalLogin($web_user);
  46. $this->drupalGet('node/add/' . $type->id());
  47. $this->assertResponse(200, 'The new content type can be accessed at node/add.');
  48. // Create a content type via the user interface.
  49. $web_user = $this->drupalCreateUser(['bypass node access', 'administer content types']);
  50. $this->drupalLogin($web_user);
  51. $this->drupalGet('node/add');
  52. $this->assertCacheTag('config:node_type_list');
  53. $this->assertCacheContext('user.permissions');
  54. $elements = $this->cssSelect('dl.node-type-list dt');
  55. $this->assertEqual(3, count($elements));
  56. $edit = [
  57. 'name' => 'foo',
  58. 'title_label' => 'title for foo',
  59. 'type' => 'foo',
  60. ];
  61. $this->drupalPostForm('admin/structure/types/add', $edit, t('Save and manage fields'));
  62. $type_exists = (bool) NodeType::load('foo');
  63. $this->assertTrue($type_exists, 'The new content type has been created in the database.');
  64. $this->drupalGet('node/add');
  65. $elements = $this->cssSelect('dl.node-type-list dt');
  66. $this->assertEqual(4, count($elements));
  67. }
  68. /**
  69. * Tests editing a node type using the UI.
  70. */
  71. public function testNodeTypeEditing() {
  72. $this->drupalPlaceBlock('system_breadcrumb_block');
  73. $web_user = $this->drupalCreateUser(['bypass node access', 'administer content types', 'administer node fields']);
  74. $this->drupalLogin($web_user);
  75. $field = FieldConfig::loadByName('node', 'page', 'body');
  76. $this->assertEqual($field->getLabel(), 'Body', 'Body field was found.');
  77. // Verify that title and body fields are displayed.
  78. $this->drupalGet('node/add/page');
  79. $this->assertRaw('Title', 'Title field was found.');
  80. $this->assertRaw('Body', 'Body field was found.');
  81. // Rename the title field.
  82. $edit = [
  83. 'title_label' => 'Foo',
  84. ];
  85. $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
  86. $this->drupalGet('node/add/page');
  87. $this->assertRaw('Foo', 'New title label was displayed.');
  88. $this->assertNoRaw('Title', 'Old title label was not displayed.');
  89. // Change the name and the description.
  90. $edit = [
  91. 'name' => 'Bar',
  92. 'description' => 'Lorem ipsum.',
  93. ];
  94. $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
  95. $this->drupalGet('node/add');
  96. $this->assertRaw('Bar', 'New name was displayed.');
  97. $this->assertRaw('Lorem ipsum', 'New description was displayed.');
  98. $this->clickLink('Bar');
  99. $this->assertRaw('Foo', 'Title field was found.');
  100. $this->assertRaw('Body', 'Body field was found.');
  101. // Change the name through the API
  102. /** @var \Drupal\node\NodeTypeInterface $node_type */
  103. $node_type = NodeType::load('page');
  104. $node_type->set('name', 'NewBar');
  105. $node_type->save();
  106. /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
  107. $bundle_info = \Drupal::service('entity_type.bundle.info');
  108. $node_bundles = $bundle_info->getBundleInfo('node');
  109. $this->assertEqual($node_bundles['page']['label'], 'NewBar', 'Node type bundle cache is updated');
  110. // Remove the body field.
  111. $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.body/delete', [], t('Delete'));
  112. // Resave the settings for this type.
  113. $this->drupalPostForm('admin/structure/types/manage/page', [], t('Save content type'));
  114. $front_page_path = Url::fromRoute('<front>')->toString();
  115. $this->assertBreadcrumb('admin/structure/types/manage/page/fields', [
  116. $front_page_path => 'Home',
  117. 'admin/structure/types' => 'Content types',
  118. 'admin/structure/types/manage/page' => 'NewBar',
  119. ]);
  120. // Check that the body field doesn't exist.
  121. $this->drupalGet('node/add/page');
  122. $this->assertNoRaw('Body', 'Body field was not found.');
  123. }
  124. /**
  125. * Tests deleting a content type that still has content.
  126. */
  127. public function testNodeTypeDeletion() {
  128. $this->drupalPlaceBlock('page_title_block');
  129. // Create a content type programmatically.
  130. $type = $this->drupalCreateContentType();
  131. // Log in a test user.
  132. $web_user = $this->drupalCreateUser([
  133. 'bypass node access',
  134. 'administer content types',
  135. ]);
  136. $this->drupalLogin($web_user);
  137. // Add a new node of this type.
  138. $node = $this->drupalCreateNode(['type' => $type->id()]);
  139. // Attempt to delete the content type, which should not be allowed.
  140. $this->drupalGet('admin/structure/types/manage/' . $type->label() . '/delete');
  141. $this->assertRaw(
  142. t('%type is used by 1 piece of content on your site. You can not remove this content type until you have removed all of the %type content.', ['%type' => $type->label()]),
  143. 'The content type will not be deleted until all nodes of that type are removed.'
  144. );
  145. $this->assertNoText(t('This action cannot be undone.'), 'The node type deletion confirmation form is not available.');
  146. // Delete the node.
  147. $node->delete();
  148. // Attempt to delete the content type, which should now be allowed.
  149. $this->drupalGet('admin/structure/types/manage/' . $type->label() . '/delete');
  150. $this->assertRaw(
  151. t('Are you sure you want to delete the content type %type?', ['%type' => $type->label()]),
  152. 'The content type is available for deletion.'
  153. );
  154. $this->assertText(t('This action cannot be undone.'), 'The node type deletion confirmation form is available.');
  155. // Test that a locked node type could not be deleted.
  156. $this->container->get('module_installer')->install(['node_test_config']);
  157. // Lock the default node type.
  158. $locked = \Drupal::state()->get('node.type.locked');
  159. $locked['default'] = 'default';
  160. \Drupal::state()->set('node.type.locked', $locked);
  161. // Call to flush all caches after installing the forum module in the same
  162. // way installing a module through the UI does.
  163. $this->resetAll();
  164. $this->drupalGet('admin/structure/types/manage/default');
  165. $this->assertNoLink(t('Delete'));
  166. $this->drupalGet('admin/structure/types/manage/default/delete');
  167. $this->assertResponse(403);
  168. $this->container->get('module_installer')->uninstall(['node_test_config']);
  169. $this->container = \Drupal::getContainer();
  170. unset($locked['default']);
  171. \Drupal::state()->set('node.type.locked', $locked);
  172. $this->drupalGet('admin/structure/types/manage/default');
  173. $this->clickLink(t('Delete'));
  174. $this->assertResponse(200);
  175. $this->drupalPostForm(NULL, [], t('Delete'));
  176. $this->assertFalse((bool) NodeType::load('default'), 'Node type with machine default deleted.');
  177. }
  178. /**
  179. * Tests Field UI integration for content types.
  180. */
  181. public function testNodeTypeFieldUiPermissions() {
  182. // Create an admin user who can only manage node fields.
  183. $admin_user_1 = $this->drupalCreateUser(['administer content types', 'administer node fields']);
  184. $this->drupalLogin($admin_user_1);
  185. // Test that the user only sees the actions available to him.
  186. $this->drupalGet('admin/structure/types');
  187. $this->assertLinkByHref('admin/structure/types/manage/article/fields');
  188. $this->assertNoLinkByHref('admin/structure/types/manage/article/display');
  189. // Create another admin user who can manage node fields display.
  190. $admin_user_2 = $this->drupalCreateUser(['administer content types', 'administer node display']);
  191. $this->drupalLogin($admin_user_2);
  192. // Test that the user only sees the actions available to him.
  193. $this->drupalGet('admin/structure/types');
  194. $this->assertNoLinkByHref('admin/structure/types/manage/article/fields');
  195. $this->assertLinkByHref('admin/structure/types/manage/article/display');
  196. }
  197. /**
  198. * Tests for when there are no content types defined.
  199. */
  200. public function testNodeTypeNoContentType() {
  201. /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
  202. $bundle_info = \Drupal::service('entity_type.bundle.info');
  203. $this->assertEqual(2, count($bundle_info->getBundleInfo('node')), 'The bundle information service has 2 bundles for the Node entity type.');
  204. $web_user = $this->drupalCreateUser(['administer content types']);
  205. $this->drupalLogin($web_user);
  206. // Delete 'article' bundle.
  207. $this->drupalPostForm('admin/structure/types/manage/article/delete', [], t('Delete'));
  208. // Delete 'page' bundle.
  209. $this->drupalPostForm('admin/structure/types/manage/page/delete', [], t('Delete'));
  210. // Navigate to content type administration screen
  211. $this->drupalGet('admin/structure/types');
  212. $this->assertRaw(t('No content types available. <a href=":link">Add content type</a>.', [
  213. ':link' => Url::fromRoute('node.type_add')->toString(),
  214. ]), 'Empty text when there are no content types in the system is correct.');
  215. $bundle_info->clearCachedBundles();
  216. $this->assertEqual(0, count($bundle_info->getBundleInfo('node')), 'The bundle information service has 0 bundles for the Node entity type.');
  217. }
  218. }