trait WorkspaceTestUtilities

Same name in other branches
  1. 9 core/modules/workspaces/tests/src/Functional/WorkspaceTestUtilities.php \Drupal\Tests\workspaces\Functional\WorkspaceTestUtilities
  2. 8.9.x core/modules/workspaces/tests/src/Functional/WorkspaceTestUtilities.php \Drupal\Tests\workspaces\Functional\WorkspaceTestUtilities
  3. 11.x core/modules/workspaces/tests/src/Functional/WorkspaceTestUtilities.php \Drupal\Tests\workspaces\Functional\WorkspaceTestUtilities

Utility methods for use in BrowserTestBase tests.

This trait will not work if not used in a child of BrowserTestBase.

Hierarchy

2 files declare their use of WorkspaceTestUtilities
WorkspaceContentModerationIntegrationTest.php in core/modules/content_moderation/tests/src/Functional/WorkspaceContentModerationIntegrationTest.php
WorkspacesLayoutBuilderIntegrationTest.php in core/modules/workspaces/tests/src/FunctionalJavascript/WorkspacesLayoutBuilderIntegrationTest.php

File

core/modules/workspaces/tests/src/Functional/WorkspaceTestUtilities.php, line 17

Namespace

Drupal\Tests\workspaces\Functional
View source
trait WorkspaceTestUtilities {
    use BlockCreationTrait;
    protected $switcherBlockConfigured = FALSE;
    
    /**
     * Loads a single entity by its label.
     *
     * The UI approach to creating an entity doesn't make it easy to know what
     * the ID is, so this lets us make paths for an entity after it's created.
     *
     * @param string $type
     *   The type of entity to load.
     * @param string $label
     *   The label of the entity to load.
     *
     * @return \Drupal\Core\Entity\EntityInterface
     *   The entity.
     */
    protected function getOneEntityByLabel($type, $label) {
        
        /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
        $entity_type_manager = \Drupal::service('entity_type.manager');
        $property = $entity_type_manager->getDefinition($type)
            ->getKey('label');
        $entity_list = $entity_type_manager->getStorage($type)
            ->loadByProperties([
            $property => $label,
        ]);
        $entity = current($entity_list);
        if (!$entity) {
            $this->fail("No {$type} entity named {$label} found.");
        }
        return $entity;
    }
    
    /**
     * Creates and activates a new Workspace through the UI.
     *
     * @param string $label
     *   The label of the workspace to create.
     * @param string $id
     *   The ID of the workspace to create.
     * @param string $parent
     *   (optional) The ID of the parent workspace. Defaults to '_none'.
     *
     * @return \Drupal\workspaces\WorkspaceInterface
     *   The workspace that was just created.
     */
    protected function createAndActivateWorkspaceThroughUi(string $label, string $id, string $parent = '_none') : WorkspaceInterface {
        $this->drupalGet('/admin/config/workflow/workspaces/add');
        $this->submitForm([
            'id' => $id,
            'label' => $label,
            'parent' => $parent,
        ], 'Save and switch');
        $this->getSession()
            ->getPage()
            ->hasContent("{$label} ({$id})");
        return Workspace::load($id);
    }
    
    /**
     * Creates a new Workspace through the UI.
     *
     * @param string $label
     *   The label of the workspace to create.
     * @param string $id
     *   The ID of the workspace to create.
     * @param string $parent
     *   (optional) The ID of the parent workspace. Defaults to '_none'.
     *
     * @return \Drupal\workspaces\WorkspaceInterface
     *   The workspace that was just created.
     */
    protected function createWorkspaceThroughUi($label, $id, $parent = '_none') {
        $this->drupalGet('/admin/config/workflow/workspaces/add');
        $this->submitForm([
            'id' => $id,
            'label' => $label,
            'parent' => $parent,
        ], 'Save');
        $this->getSession()
            ->getPage()
            ->hasContent("{$label} ({$id})");
        return Workspace::load($id);
    }
    
    /**
     * Adds the workspace switcher block to the site.
     *
     * This is necessary for switchToWorkspace() to function correctly.
     */
    protected function setupWorkspaceSwitcherBlock() {
        // Add the block to the sidebar.
        $this->placeBlock('workspace_switcher', [
            'id' => 'workspace_switcher',
            'region' => 'sidebar_first',
            'label' => 'Workspace switcher',
        ]);
        $this->drupalGet('<front>');
        $this->switcherBlockConfigured = TRUE;
    }
    
    /**
     * Sets a given workspace as "active" for subsequent requests.
     *
     * This assumes that the switcher block has already been setup by calling
     * setupWorkspaceSwitcherBlock().
     *
     * @param \Drupal\workspaces\WorkspaceInterface $workspace
     *   The workspace to set active.
     */
    protected function switchToWorkspace(WorkspaceInterface $workspace) {
        $this->assertTrue($this->switcherBlockConfigured, 'This test was not written correctly: you must call setupWorkspaceSwitcherBlock() before switchToWorkspace()');
        
        /** @var \Drupal\Tests\WebAssert $session */
        $session = $this->assertSession();
        $session->buttonExists('Activate');
        $this->submitForm([
            'workspace_id' => $workspace->id(),
        ], 'Activate');
        $session->pageTextContains($workspace->label() . ' is now the active workspace.');
        // Keep the test runner in sync with the system under test.
        \Drupal::service('workspaces.manager')->setActiveWorkspace($workspace);
    }
    
    /**
     * Switches to the live version of the site for subsequent requests.
     *
     * This assumes that the switcher block has already been setup by calling
     * setupWorkspaceSwitcherBlock().
     */
    protected function switchToLive() {
        
        /** @var \Drupal\Tests\WebAssert $session */
        $session = $this->assertSession();
        $this->submitForm([], 'Switch to Live');
        $session->pageTextContains('You are now viewing the live version of the site.');
        // Keep the test runner in sync with the system under test.
        \Drupal::service('workspaces.manager')->switchToLive();
    }
    
    /**
     * Creates a node by "clicking" buttons.
     *
     * @param string $label
     *   The label of the Node to create.
     * @param string $bundle
     *   The bundle of the Node to create.
     * @param bool $publish
     *   The publishing status to set.
     *
     * @return \Drupal\node\NodeInterface
     *   The Node that was just created.
     *
     * @throws \Behat\Mink\Exception\ElementNotFoundException
     */
    protected function createNodeThroughUi($label, $bundle, $publish = TRUE) {
        $this->drupalGet('/node/add/' . $bundle);
        
        /** @var \Behat\Mink\Session $session */
        $session = $this->getSession();
        $this->assertSession()
            ->statusCodeEquals(200);
        
        /** @var \Behat\Mink\Element\DocumentElement $page */
        $page = $session->getPage();
        $page->fillField('Title', $label);
        if ($publish) {
            $page->findButton('Save')
                ->click();
        }
        else {
            $page->uncheckField('Published');
            $page->findButton('Save')
                ->click();
        }
        $session->getPage()
            ->hasContent("{$label} has been created");
        return $this->getOneEntityByLabel('node', $label);
    }
    
    /**
     * Determine if the content list has an entity's label.
     *
     * This assertion can be used to validate a particular entity exists in the
     * current workspace.
     */
    protected function isLabelInContentOverview($label) {
        $this->drupalGet('/admin/content');
        $session = $this->getSession();
        $this->assertSession()
            ->statusCodeEquals(200);
        $page = $session->getPage();
        return $page->hasContent($label);
    }
    
    /**
     * Marks an entity type as ignored in a workspace.
     *
     * @param string $entity_type_id
     *   The entity type ID.
     */
    protected function ignoreEntityType(string $entity_type_id) : void {
        $entity_type = clone \Drupal::entityTypeManager()->getDefinition($entity_type_id);
        $entity_type->setHandlerClass('workspace', IgnoredWorkspaceHandler::class);
        \Drupal::state()->set("{$entity_type_id}.entity_type", $entity_type);
        \Drupal::entityTypeManager()->clearCachedDefinitions();
    }

}

Members

Title Sort descending Modifiers Object type Summary
BlockCreationTrait::placeBlock protected function Creates a block instance based on default settings.
WorkspaceTestUtilities::$switcherBlockConfigured protected property
WorkspaceTestUtilities::createAndActivateWorkspaceThroughUi protected function Creates and activates a new Workspace through the UI.
WorkspaceTestUtilities::createNodeThroughUi protected function Creates a node by &quot;clicking&quot; buttons.
WorkspaceTestUtilities::createWorkspaceThroughUi protected function Creates a new Workspace through the UI.
WorkspaceTestUtilities::getOneEntityByLabel protected function Loads a single entity by its label.
WorkspaceTestUtilities::ignoreEntityType protected function Marks an entity type as ignored in a workspace.
WorkspaceTestUtilities::isLabelInContentOverview protected function Determine if the content list has an entity&#039;s label.
WorkspaceTestUtilities::setupWorkspaceSwitcherBlock protected function Adds the workspace switcher block to the site.
WorkspaceTestUtilities::switchToLive protected function Switches to the live version of the site for subsequent requests.
WorkspaceTestUtilities::switchToWorkspace protected function Sets a given workspace as &quot;active&quot; for subsequent requests.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.