NodeTypeTest.php 10 KB

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