3 namespace Drupal\node\Tests;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\node\Entity\NodeType;
8 use Drupal\system\Tests\Menu\AssertBreadcrumbTrait;
11 * Ensures that node type functions work correctly.
15 class NodeTypeTest extends NodeTestBase {
17 use AssertBreadcrumbTrait;
24 public static $modules = ['field_ui', 'block'];
27 * Ensures that node type functions (node_type_get_*) work correctly.
29 * Load available node types and validate the returned data.
31 public function testNodeTypeGetFunctions() {
32 $node_types = NodeType::loadMultiple();
33 $node_names = node_type_get_names();
35 $this->assertTrue(isset($node_types['article']), 'Node type article is available.');
36 $this->assertTrue(isset($node_types['page']), 'Node type basic page is available.');
38 $this->assertEqual($node_types['article']->label(), $node_names['article'], 'Correct node type base has been returned.');
40 $article = NodeType::load('article');
41 $this->assertEqual($node_types['article'], $article, 'Correct node type has been returned.');
42 $this->assertEqual($node_types['article']->label(), $article->label(), 'Correct node type name has been returned.');
46 * Tests creating a content type programmatically and via a form.
48 public function testNodeTypeCreation() {
49 // Create a content type programmatically.
50 $type = $this->drupalCreateContentType();
52 $type_exists = (bool) NodeType::load($type->id());
53 $this->assertTrue($type_exists, 'The new content type has been created in the database.');
55 // Log in a test user.
56 $web_user = $this->drupalCreateUser(['create ' . $type->label() . ' content']);
57 $this->drupalLogin($web_user);
59 $this->drupalGet('node/add/' . $type->id());
60 $this->assertResponse(200, 'The new content type can be accessed at node/add.');
62 // Create a content type via the user interface.
63 $web_user = $this->drupalCreateUser(['bypass node access', 'administer content types']);
64 $this->drupalLogin($web_user);
66 $this->drupalGet('node/add');
67 $this->assertCacheTag('config:node_type_list');
68 $this->assertCacheContext('user.permissions');
69 $elements = $this->cssSelect('dl.node-type-list dt');
70 $this->assertEqual(3, count($elements));
74 'title_label' => 'title for foo',
77 $this->drupalPostForm('admin/structure/types/add', $edit, t('Save and manage fields'));
78 $type_exists = (bool) NodeType::load('foo');
79 $this->assertTrue($type_exists, 'The new content type has been created in the database.');
81 $this->drupalGet('node/add');
82 $elements = $this->cssSelect('dl.node-type-list dt');
83 $this->assertEqual(4, count($elements));
87 * Tests editing a node type using the UI.
89 public function testNodeTypeEditing() {
90 $this->drupalPlaceBlock('system_breadcrumb_block');
91 $web_user = $this->drupalCreateUser(['bypass node access', 'administer content types', 'administer node fields']);
92 $this->drupalLogin($web_user);
94 $field = FieldConfig::loadByName('node', 'page', 'body');
95 $this->assertEqual($field->getLabel(), 'Body', 'Body field was found.');
97 // Verify that title and body fields are displayed.
98 $this->drupalGet('node/add/page');
99 $this->assertRaw('Title', 'Title field was found.');
100 $this->assertRaw('Body', 'Body field was found.');
102 // Rename the title field.
104 'title_label' => 'Foo',
106 $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
108 $this->drupalGet('node/add/page');
109 $this->assertRaw('Foo', 'New title label was displayed.');
110 $this->assertNoRaw('Title', 'Old title label was not displayed.');
112 // Change the name and the description.
115 'description' => 'Lorem ipsum.',
117 $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
119 $this->drupalGet('node/add');
120 $this->assertRaw('Bar', 'New name was displayed.');
121 $this->assertRaw('Lorem ipsum', 'New description was displayed.');
122 $this->clickLink('Bar');
123 $this->assertRaw('Foo', 'Title field was found.');
124 $this->assertRaw('Body', 'Body field was found.');
126 // Change the name through the API
127 /** @var \Drupal\node\NodeTypeInterface $node_type */
128 $node_type = NodeType::load('page');
129 $node_type->set('name', 'NewBar');
132 /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
133 $bundle_info = \Drupal::service('entity_type.bundle.info');
134 $node_bundles = $bundle_info->getBundleInfo('node');
135 $this->assertEqual($node_bundles['page']['label'], 'NewBar', 'Node type bundle cache is updated');
137 // Remove the body field.
138 $this->drupalPostForm('admin/structure/types/manage/page/fields/node.page.body/delete', [], t('Delete'));
139 // Resave the settings for this type.
140 $this->drupalPostForm('admin/structure/types/manage/page', [], t('Save content type'));
141 $front_page_path = Url::fromRoute('<front>')->toString();
142 $this->assertBreadcrumb('admin/structure/types/manage/page/fields', [
143 $front_page_path => 'Home',
144 'admin/structure/types' => 'Content types',
145 'admin/structure/types/manage/page' => 'NewBar',
147 // Check that the body field doesn't exist.
148 $this->drupalGet('node/add/page');
149 $this->assertNoRaw('Body', 'Body field was not found.');
153 * Tests deleting a content type that still has content.
155 public function testNodeTypeDeletion() {
156 $this->drupalPlaceBlock('page_title_block');
157 // Create a content type programmatically.
158 $type = $this->drupalCreateContentType();
160 // Log in a test user.
161 $web_user = $this->drupalCreateUser([
162 'bypass node access',
163 'administer content types',
165 $this->drupalLogin($web_user);
167 // Add a new node of this type.
168 $node = $this->drupalCreateNode(['type' => $type->id()]);
169 // Attempt to delete the content type, which should not be allowed.
170 $this->drupalGet('admin/structure/types/manage/' . $type->label() . '/delete');
172 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()]),
173 'The content type will not be deleted until all nodes of that type are removed.'
175 $this->assertNoText(t('This action cannot be undone.'), 'The node type deletion confirmation form is not available.');
179 // Attempt to delete the content type, which should now be allowed.
180 $this->drupalGet('admin/structure/types/manage/' . $type->label() . '/delete');
182 t('Are you sure you want to delete the content type %type?', ['%type' => $type->label()]),
183 'The content type is available for deletion.'
185 $this->assertText(t('This action cannot be undone.'), 'The node type deletion confirmation form is available.');
187 // Test that a locked node type could not be deleted.
188 $this->container->get('module_installer')->install(['node_test_config']);
189 // Lock the default node type.
190 $locked = \Drupal::state()->get('node.type.locked');
191 $locked['default'] = 'default';
192 \Drupal::state()->set('node.type.locked', $locked);
193 // Call to flush all caches after installing the forum module in the same
194 // way installing a module through the UI does.
196 $this->drupalGet('admin/structure/types/manage/default');
197 $this->assertNoLink(t('Delete'));
198 $this->drupalGet('admin/structure/types/manage/default/delete');
199 $this->assertResponse(403);
200 $this->container->get('module_installer')->uninstall(['node_test_config']);
201 $this->container = \Drupal::getContainer();
202 unset($locked['default']);
203 \Drupal::state()->set('node.type.locked', $locked);
204 $this->drupalGet('admin/structure/types/manage/default');
205 $this->clickLink(t('Delete'));
206 $this->assertResponse(200);
207 $this->drupalPostForm(NULL, [], t('Delete'));
208 $this->assertFalse((bool) NodeType::load('default'), 'Node type with machine default deleted.');
212 * Tests Field UI integration for content types.
214 public function testNodeTypeFieldUiPermissions() {
215 // Create an admin user who can only manage node fields.
216 $admin_user_1 = $this->drupalCreateUser(['administer content types', 'administer node fields']);
217 $this->drupalLogin($admin_user_1);
219 // Test that the user only sees the actions available to him.
220 $this->drupalGet('admin/structure/types');
221 $this->assertLinkByHref('admin/structure/types/manage/article/fields');
222 $this->assertNoLinkByHref('admin/structure/types/manage/article/display');
224 // Create another admin user who can manage node fields display.
225 $admin_user_2 = $this->drupalCreateUser(['administer content types', 'administer node display']);
226 $this->drupalLogin($admin_user_2);
228 // Test that the user only sees the actions available to him.
229 $this->drupalGet('admin/structure/types');
230 $this->assertNoLinkByHref('admin/structure/types/manage/article/fields');
231 $this->assertLinkByHref('admin/structure/types/manage/article/display');
235 * Tests for when there are no content types defined.
237 public function testNodeTypeNoContentType() {
238 /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
239 $bundle_info = \Drupal::service('entity_type.bundle.info');
240 $this->assertEqual(2, count($bundle_info->getBundleInfo('node')), 'The bundle information service has 2 bundles for the Node entity type.');
241 $web_user = $this->drupalCreateUser(['administer content types']);
242 $this->drupalLogin($web_user);
244 // Delete 'article' bundle.
245 $this->drupalPostForm('admin/structure/types/manage/article/delete', [], t('Delete'));
246 // Delete 'page' bundle.
247 $this->drupalPostForm('admin/structure/types/manage/page/delete', [], t('Delete'));
249 // Navigate to content type administration screen
250 $this->drupalGet('admin/structure/types');
251 $this->assertRaw(t('No content types available. <a href=":link">Add content type</a>.', [
252 ':link' => Url::fromRoute('node.type_add')->toString()
253 ]), 'Empty text when there are no content types in the system is correct.');
255 $bundle_info->clearCachedBundles();
256 $this->assertEqual(0, count($bundle_info->getBundleInfo('node')), 'The bundle information service has 0 bundles for the Node entity type.');