3 namespace Drupal\Tests\workspaces\Functional;
5 use Drupal\Tests\block\Traits\BlockCreationTrait;
6 use Drupal\workspaces\Entity\Workspace;
7 use Drupal\workspaces\WorkspaceInterface;
10 * Utility methods for use in BrowserTestBase tests.
12 * This trait will not work if not used in a child of BrowserTestBase.
14 trait WorkspaceTestUtilities {
16 use BlockCreationTrait;
19 * Loads a single entity by its label.
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.
25 * The type of entity to load.
26 * @param string $label
27 * The label of the entity to load.
29 * @return \Drupal\Core\Entity\EntityInterface
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);
39 $this->fail("No {$type} entity named {$label} found.");
46 * Creates a new Workspace through the UI.
48 * @param string $label
49 * The label of the workspace to create.
51 * The ID of the workspace to create.
53 * @return \Drupal\workspaces\WorkspaceInterface
54 * The workspace that was just created.
56 protected function createWorkspaceThroughUi($label, $id) {
57 $this->drupalPostForm('/admin/config/workflow/workspaces/add', [
62 $this->getSession()->getPage()->hasContent("$label ($id)");
64 return Workspace::load($id);
68 * Adds the workspace switcher block to the site.
70 * This is necessary for switchToWorkspace() to function correctly.
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',
80 // Confirm the block shows on the front page.
81 $this->drupalGet('<front>');
82 $page = $this->getSession()->getPage();
84 $this->assertTrue($page->hasContent('Workspace switcher'));
88 * Sets a given workspace as "active" for subsequent requests.
90 * This assumes that the switcher block has already been setup by calling
91 * setupWorkspaceSwitcherBlock().
93 * @param \Drupal\workspaces\WorkspaceInterface $workspace
94 * The workspace to set active.
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.');
105 * Creates a node by "clicking" buttons.
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.
114 * @return \Drupal\node\NodeInterface
115 * The Node that was just created.
117 * @throws \Behat\Mink\Exception\ElementNotFoundException
119 protected function createNodeThroughUi($label, $bundle, $publish = TRUE) {
120 $this->drupalGet('/node/add/' . $bundle);
122 /** @var \Behat\Mink\Session $session */
123 $session = $this->getSession();
124 $this->assertSession()->statusCodeEquals(200);
126 /** @var \Behat\Mink\Element\DocumentElement $page */
127 $page = $session->getPage();
128 $page->fillField('Title', $label);
130 $page->findButton('Save')->click();
133 $page->uncheckField('Published');
134 $page->findButton('Save')->click();
137 $session->getPage()->hasContent("{$label} has been created");
139 return $this->getOneEntityByLabel('node', $label);
143 * Determine if the content list has an entity's label.
145 * This assertion can be used to validate a particular entity exists in the
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);