Version 1
[yaffs-website] / web / core / modules / node / src / Tests / NodeTypeTest.php
1 <?php
2
3 namespace Drupal\node\Tests;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Core\Url;
8 use Drupal\system\Tests\Menu\AssertBreadcrumbTrait;
9
10 /**
11  * Ensures that node type functions work correctly.
12  *
13  * @group node
14  */
15 class NodeTypeTest extends NodeTestBase {
16
17   use AssertBreadcrumbTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['field_ui', 'block'];
25
26   /**
27    * Ensures that node type functions (node_type_get_*) work correctly.
28    *
29    * Load available node types and validate the returned data.
30    */
31   public function testNodeTypeGetFunctions() {
32     $node_types = NodeType::loadMultiple();
33     $node_names = node_type_get_names();
34
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.');
37
38     $this->assertEqual($node_types['article']->label(), $node_names['article'], 'Correct node type base has been returned.');
39
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.');
43   }
44
45   /**
46    * Tests creating a content type programmatically and via a form.
47    */
48   public function testNodeTypeCreation() {
49     // Create a content type programmatically.
50     $type = $this->drupalCreateContentType();
51
52     $type_exists = (bool) NodeType::load($type->id());
53     $this->assertTrue($type_exists, 'The new content type has been created in the database.');
54
55     // Log in a test user.
56     $web_user = $this->drupalCreateUser(['create ' . $type->label() . ' content']);
57     $this->drupalLogin($web_user);
58
59     $this->drupalGet('node/add/' . $type->id());
60     $this->assertResponse(200, 'The new content type can be accessed at node/add.');
61
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);
65
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));
71
72     $edit = [
73       'name' => 'foo',
74       'title_label' => 'title for foo',
75       'type' => 'foo',
76     ];
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.');
80
81     $this->drupalGet('node/add');
82     $elements = $this->cssSelect('dl.node-type-list dt');
83     $this->assertEqual(4, count($elements));
84   }
85
86   /**
87    * Tests editing a node type using the UI.
88    */
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);
93
94     $field = FieldConfig::loadByName('node', 'page', 'body');
95     $this->assertEqual($field->getLabel(), 'Body', 'Body field was found.');
96
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.');
101
102     // Rename the title field.
103     $edit = [
104       'title_label' => 'Foo',
105     ];
106     $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
107
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.');
111
112     // Change the name and the description.
113     $edit = [
114       'name' => 'Bar',
115       'description' => 'Lorem ipsum.',
116     ];
117     $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
118
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.');
125
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');
130     $node_type->save();
131
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');
136
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',
146     ]);
147     // Check that the body field doesn't exist.
148     $this->drupalGet('node/add/page');
149     $this->assertNoRaw('Body', 'Body field was not found.');
150   }
151
152   /**
153    * Tests deleting a content type that still has content.
154    */
155   public function testNodeTypeDeletion() {
156     $this->drupalPlaceBlock('page_title_block');
157     // Create a content type programmatically.
158     $type = $this->drupalCreateContentType();
159
160     // Log in a test user.
161     $web_user = $this->drupalCreateUser([
162       'bypass node access',
163       'administer content types',
164     ]);
165     $this->drupalLogin($web_user);
166
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');
171     $this->assertRaw(
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.'
174     );
175     $this->assertNoText(t('This action cannot be undone.'), 'The node type deletion confirmation form is not available.');
176
177     // Delete the node.
178     $node->delete();
179     // Attempt to delete the content type, which should now be allowed.
180     $this->drupalGet('admin/structure/types/manage/' . $type->label() . '/delete');
181     $this->assertRaw(
182       t('Are you sure you want to delete the content type %type?', ['%type' => $type->label()]),
183       'The content type is available for deletion.'
184     );
185     $this->assertText(t('This action cannot be undone.'), 'The node type deletion confirmation form is available.');
186
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.
195     $this->resetAll();
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.');
209   }
210
211   /**
212    * Tests Field UI integration for content types.
213    */
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);
218
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');
223
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);
227
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');
232   }
233
234   /**
235    * Tests for when there are no content types defined.
236    */
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);
243
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'));
248
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.');
254
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.');
257   }
258
259 }