9bab0b54312c7edc4730f93f6d24f00efcd22f99
[yaffs-website] / web / core / modules / field_layout / tests / src / Functional / FieldLayoutTest.php
1 <?php
2
3 namespace Drupal\Tests\field_layout\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests using field layout for entity displays.
9  *
10  * @group field_layout
11  */
12 class FieldLayoutTest extends BrowserTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['field_layout', 'field_ui', 'node', 'field_layout_test'];
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     parent::setUp();
24
25     $this->createContentType([
26       'type' => 'article',
27     ]);
28     $this->createNode([
29       'type' => 'article',
30       'title' => 'The node title',
31       'body' => [
32         ['value' => 'The node body'],
33       ],
34     ]);
35     $this->drupalLogin($this->drupalCreateUser([
36       'access administration pages',
37       'administer content types',
38       'administer nodes',
39       'administer node fields',
40       'administer node display',
41       'administer node form display',
42       'view the administration theme',
43     ]));
44   }
45
46   /**
47    * Tests an entity type that has fields shown by default.
48    */
49   public function testNodeView() {
50     // By default, the one-column layout is used.
51     $this->drupalGet('node/1');
52     $this->assertSession()->elementExists('css', '.layout--onecol');
53     $this->assertSession()->elementExists('css', '.layout__region--content .field--name-body');
54
55     $this->drupalGet('admin/structure/types/manage/article/display');
56     $this->assertEquals(['Content', 'Disabled'], $this->getRegionTitles());
57     $this->assertSession()->optionExists('fields[body][region]', 'content');
58   }
59
60   /**
61    * Tests that changes to the regions still leave the fields visible.
62    */
63   public function testRegionChanges() {
64     $this->drupalGet('admin/structure/types/manage/article/display');
65     $this->assertEquals(['Content', 'Disabled'], $this->getRegionTitles());
66     $this->assertSession()->optionExists('fields[body][region]', 'content');
67
68     \Drupal::state()->set('field_layout_test.alter_regions', TRUE);
69     \Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
70
71     $this->drupalGet('admin/structure/types/manage/article/display');
72     $this->assertEquals(['Foo', 'Disabled'], $this->getRegionTitles());
73     $this->assertSession()->optionExists('fields[body][region]', 'hidden');
74   }
75
76   /**
77    * Gets the region titles on the page.
78    *
79    * @return string[]
80    *   An array of region titles.
81    */
82   protected function getRegionTitles() {
83     $region_titles = [];
84     $region_title_elements = $this->getSession()->getPage()->findAll('css', '.region-title td');
85     /** @var \Behat\Mink\Element\NodeElement[] $region_title_elements */
86     foreach ($region_title_elements as $region_title_element) {
87       $region_titles[] = $region_title_element->getText();
88     }
89     return $region_titles;
90   }
91
92 }