PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/node/src/Tests/NodeTypeTest.php

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