Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / workspaces / tests / src / Functional / WorkspaceTestUtilities.php
1 <?php
2
3 namespace Drupal\Tests\workspaces\Functional;
4
5 use Drupal\Tests\block\Traits\BlockCreationTrait;
6 use Drupal\workspaces\Entity\Workspace;
7 use Drupal\workspaces\WorkspaceInterface;
8
9 /**
10  * Utility methods for use in BrowserTestBase tests.
11  *
12  * This trait will not work if not used in a child of BrowserTestBase.
13  */
14 trait WorkspaceTestUtilities {
15
16   use BlockCreationTrait;
17
18   /**
19    * Loads a single entity by its label.
20    *
21    * The UI approach to creating an entity doesn't make it easy to know what
22    * the ID is, so this lets us make paths for an entity after it's created.
23    *
24    * @param string $type
25    *   The type of entity to load.
26    * @param string $label
27    *   The label of the entity to load.
28    *
29    * @return \Drupal\Core\Entity\EntityInterface
30    *   The entity.
31    */
32   protected function getOneEntityByLabel($type, $label) {
33     /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
34     $entity_type_manager = \Drupal::service('entity_type.manager');
35     $property = $entity_type_manager->getDefinition($type)->getKey('label');
36     $entity_list = $entity_type_manager->getStorage($type)->loadByProperties([$property => $label]);
37     $entity = current($entity_list);
38     if (!$entity) {
39       $this->fail("No {$type} entity named {$label} found.");
40     }
41
42     return $entity;
43   }
44
45   /**
46    * Creates a new Workspace through the UI.
47    *
48    * @param string $label
49    *   The label of the workspace to create.
50    * @param string $id
51    *   The ID of the workspace to create.
52    *
53    * @return \Drupal\workspaces\WorkspaceInterface
54    *   The workspace that was just created.
55    */
56   protected function createWorkspaceThroughUi($label, $id) {
57     $this->drupalPostForm('/admin/config/workflow/workspaces/add', [
58       'id' => $id,
59       'label' => $label,
60     ], 'Save');
61
62     $this->getSession()->getPage()->hasContent("$label ($id)");
63
64     return Workspace::load($id);
65   }
66
67   /**
68    * Adds the workspace switcher block to the site.
69    *
70    * This is necessary for switchToWorkspace() to function correctly.
71    */
72   protected function setupWorkspaceSwitcherBlock() {
73     // Add the block to the sidebar.
74     $this->placeBlock('workspace_switcher', [
75       'id' => 'workspaceswitcher',
76       'region' => 'sidebar_first',
77       'label' => 'Workspace switcher',
78     ]);
79
80     // Confirm the block shows on the front page.
81     $this->drupalGet('<front>');
82     $page = $this->getSession()->getPage();
83
84     $this->assertTrue($page->hasContent('Workspace switcher'));
85   }
86
87   /**
88    * Sets a given workspace as "active" for subsequent requests.
89    *
90    * This assumes that the switcher block has already been setup by calling
91    * setupWorkspaceSwitcherBlock().
92    *
93    * @param \Drupal\workspaces\WorkspaceInterface $workspace
94    *   The workspace to set active.
95    */
96   protected function switchToWorkspace(WorkspaceInterface $workspace) {
97     /** @var \Drupal\Tests\WebAssert $session */
98     $session = $this->assertSession();
99     $session->buttonExists('Activate');
100     $this->drupalPostForm(NULL, ['workspace_id' => $workspace->id()], 'Activate');
101     $session->pageTextContains($workspace->label() . ' is now the active workspace.');
102   }
103
104   /**
105    * Creates a node by "clicking" buttons.
106    *
107    * @param string $label
108    *   The label of the Node to create.
109    * @param string $bundle
110    *   The bundle of the Node to create.
111    * @param bool $publish
112    *   The publishing status to set.
113    *
114    * @return \Drupal\node\NodeInterface
115    *   The Node that was just created.
116    *
117    * @throws \Behat\Mink\Exception\ElementNotFoundException
118    */
119   protected function createNodeThroughUi($label, $bundle, $publish = TRUE) {
120     $this->drupalGet('/node/add/' . $bundle);
121
122     /** @var \Behat\Mink\Session $session */
123     $session = $this->getSession();
124     $this->assertSession()->statusCodeEquals(200);
125
126     /** @var \Behat\Mink\Element\DocumentElement $page */
127     $page = $session->getPage();
128     $page->fillField('Title', $label);
129     if ($publish) {
130       $page->findButton('Save')->click();
131     }
132     else {
133       $page->uncheckField('Published');
134       $page->findButton('Save')->click();
135     }
136
137     $session->getPage()->hasContent("{$label} has been created");
138
139     return $this->getOneEntityByLabel('node', $label);
140   }
141
142   /**
143    * Determine if the content list has an entity's label.
144    *
145    * This assertion can be used to validate a particular entity exists in the
146    * current workspace.
147    */
148   protected function isLabelInContentOverview($label) {
149     $this->drupalGet('/admin/content');
150     $session = $this->getSession();
151     $this->assertSession()->statusCodeEquals(200);
152     $page = $session->getPage();
153     return $page->hasContent($label);
154   }
155
156 }