Same name in this branch
  1. 10 core/modules/jsonapi/tests/src/Functional/WorkspaceTest.php \Drupal\Tests\jsonapi\Functional\WorkspaceTest
  2. 10 core/modules/workspaces/tests/src/Functional/WorkspaceTest.php \Drupal\Tests\workspaces\Functional\WorkspaceTest
Same name and namespace in other branches
  1. 8.9.x core/modules/workspaces/tests/src/Functional/WorkspaceTest.php \Drupal\Tests\workspaces\Functional\WorkspaceTest
  2. 9 core/modules/workspaces/tests/src/Functional/WorkspaceTest.php \Drupal\Tests\workspaces\Functional\WorkspaceTest

Test the workspace entity.

@group workspaces @group #slow

Hierarchy

Expanded class hierarchy of WorkspaceTest

File

core/modules/workspaces/tests/src/Functional/WorkspaceTest.php, line 18

Namespace

Drupal\Tests\workspaces\Functional
View source
class WorkspaceTest extends BrowserTestBase {
  use WorkspaceTestUtilities;
  use ContentTypeCreationTrait;
  use TaxonomyTestTrait;
  use FieldUiTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'block',
    'field_ui',
    'node',
    'taxonomy',
    'toolbar',
    'user',
    'workspaces',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * A test user.
   *
   * @var \Drupal\user\Entity\User
   */
  protected $editor1;

  /**
   * A test user.
   *
   * @var \Drupal\user\Entity\User
   */
  protected $editor2;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $permissions = [
      'access administration pages',
      'administer site configuration',
      'create workspace',
      'edit own workspace',
      'edit any workspace',
      'view own workspace',
      'access toolbar',
    ];
    $this->editor1 = $this
      ->drupalCreateUser($permissions);
    $this->editor2 = $this
      ->drupalCreateUser($permissions);
    $this
      ->drupalPlaceBlock('local_actions_block');
  }

  /**
   * Tests creating a workspace with special characters.
   */
  public function testSpecialCharacters() {
    $this
      ->drupalLogin($this->editor1);
    $page = $this
      ->getSession()
      ->getPage();

    // Test a valid workspace name.
    $this
      ->createAndActivateWorkspaceThroughUi('Workspace 1', 'workspace_1');
    $this
      ->assertSession()
      ->elementTextContains('css', '.workspaces-toolbar-tab', 'Workspace 1');

    // Test and invalid workspace name.
    $this
      ->drupalGet('/admin/config/workflow/workspaces/add');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $page
      ->fillField('label', 'workspace2');
    $page
      ->fillField('id', 'A!"£%^&*{}#~@?');
    $page
      ->findButton('Save')
      ->click();
    $page
      ->hasContent("This value is not valid");
  }

  /**
   * Tests that the toolbar correctly shows the active workspace.
   */
  public function testWorkspaceToolbar() {
    $this
      ->drupalLogin($this->editor1);
    $this
      ->drupalGet('/admin/config/workflow/workspaces/add');
    $this
      ->submitForm([
      'id' => 'test_workspace',
      'label' => 'Test workspace',
    ], 'Save');

    // Activate the test workspace.
    $this
      ->drupalGet('/admin/config/workflow/workspaces/manage/test_workspace/activate');
    $this
      ->submitForm([], 'Confirm');
    $this
      ->drupalGet('<front>');
    $page = $this
      ->getSession()
      ->getPage();

    // Toolbar should show the correct label.
    $this
      ->assertTrue($page
      ->hasLink('Test workspace'));

    // Change the workspace label.
    $this
      ->drupalGet('/admin/config/workflow/workspaces/manage/test_workspace/edit');
    $this
      ->submitForm([
      'label' => 'New name',
    ], 'Save');
    $this
      ->drupalGet('<front>');
    $page = $this
      ->getSession()
      ->getPage();

    // Toolbar should show the new label.
    $this
      ->assertTrue($page
      ->hasLink('New name'));
  }

  /**
   * Tests changing the owner of a workspace.
   */
  public function testWorkspaceOwner() {
    $this
      ->drupalLogin($this->editor1);
    $this
      ->drupalGet('/admin/config/workflow/workspaces/add');
    $this
      ->submitForm([
      'id' => 'test_workspace',
      'label' => 'Test workspace',
    ], 'Save');
    $storage = \Drupal::entityTypeManager()
      ->getStorage('workspace');
    $test_workspace = $storage
      ->load('test_workspace');
    $this
      ->assertEquals($this->editor1
      ->id(), $test_workspace
      ->getOwnerId());
    $this
      ->drupalGet('/admin/config/workflow/workspaces/manage/test_workspace/edit');
    $this
      ->submitForm([
      'uid[0][target_id]' => $this->editor2
        ->getAccountName(),
    ], 'Save');
    $test_workspace = $storage
      ->loadUnchanged('test_workspace');
    $this
      ->assertEquals($this->editor2
      ->id(), $test_workspace
      ->getOwnerId());
  }

  /**
   * Tests that editing a workspace creates a new revision.
   */
  public function testWorkspaceFormRevisions() {
    $this
      ->drupalLogin($this->editor1);
    $storage = \Drupal::entityTypeManager()
      ->getStorage('workspace');

    // The current 'stage' workspace entity should be revision 1.
    $stage_workspace = $storage
      ->load('stage');
    $this
      ->assertEquals('1', $stage_workspace
      ->getRevisionId());

    // Re-save the 'stage' workspace via the UI to create revision 2.
    $this
      ->drupalGet($stage_workspace
      ->toUrl('edit-form')
      ->toString());
    $this
      ->submitForm([], 'Save');
    $stage_workspace = $storage
      ->loadUnchanged('stage');
    $this
      ->assertEquals('2', $stage_workspace
      ->getRevisionId());
  }

  /**
   * Tests the manage workspace page.
   */
  public function testWorkspaceManagePage() {
    $this
      ->drupalCreateContentType([
      'type' => 'test',
      'label' => 'Test',
    ]);
    $permissions = [
      'administer taxonomy',
      'administer workspaces',
      'create test content',
      'delete any test content',
    ];
    $this
      ->drupalLogin($this
      ->drupalCreateUser($permissions));
    $this
      ->setupWorkspaceSwitcherBlock();
    $assert_session = $this
      ->assertSession();
    $vocabulary = $this
      ->createVocabulary();
    $test_1 = $this
      ->createWorkspaceThroughUi('Test 1', 'test_1');
    $test_2 = $this
      ->createWorkspaceThroughUi('Test 2', 'test_2');
    $this
      ->switchToWorkspace($test_1);

    // Check that the 'test_1' workspace doesn't contain any changes initially.
    $this
      ->drupalGet($test_1
      ->toUrl()
      ->toString());
    $assert_session
      ->pageTextContains('This workspace has no changes.');

    // Check that the 'Switch to this workspace' action link is not displayed on
    // the manage page of the currently active workspace.
    $assert_session
      ->linkNotExists('Switch to this workspace');
    $this
      ->drupalGet($test_2
      ->toUrl()
      ->toString());
    $assert_session
      ->linkExists('Switch to this workspace');

    // Create some test content.
    $this
      ->createNodeThroughUi('Node 1', 'test');
    $this
      ->createNodeThroughUi('Node 2', 'test');
    $edit = [
      'name[0][value]' => 'Term 1',
    ];
    $this
      ->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary
      ->id() . '/add');
    $this
      ->submitForm($edit, 'Save');
    $this
      ->drupalGet($test_1
      ->toUrl()
      ->toString());
    $assert_session
      ->pageTextContains('2 content items, 1 taxonomy term');
    $assert_session
      ->linkExists('Node 1');
    $assert_session
      ->linkExists('Node 2');
    $assert_session
      ->linkExists('Term 1');
  }

  /**
   * Tests adding new fields to workspace entities.
   */
  public function testWorkspaceFieldUi() {
    $user = $this
      ->drupalCreateUser([
      'administer workspaces',
      'access administration pages',
      'administer site configuration',
      'administer workspace fields',
      'administer workspace display',
      'administer workspace form display',
    ]);
    $this
      ->drupalLogin($user);
    $this
      ->drupalGet('admin/config/workflow/workspaces/fields');
    $this
      ->assertSession()
      ->statusCodeEquals(200);

    // Create a new filed.
    $field_name = $this
      ->randomMachineName();
    $field_label = $this
      ->randomMachineName();
    $this
      ->fieldUIAddNewField('admin/config/workflow/workspaces', $field_name, $field_label, 'string');

    // Check that the field is displayed on the manage form display page.
    $this
      ->drupalGet('admin/config/workflow/workspaces/form-display');
    $this
      ->assertSession()
      ->pageTextContains($field_label);

    // Check that the field is displayed on the manage display page.
    $this
      ->drupalGet('admin/config/workflow/workspaces/display');
    $this
      ->assertSession()
      ->pageTextContains($field_label);
  }

  /**
   * Verifies that a workspace with existing content may be deleted.
   */
  public function testDeleteWorkspaceWithExistingContent() {
    $this
      ->createContentType([
      'type' => 'test',
      'label' => 'Test',
    ]);

    // Login and create a workspace.
    $permissions = [
      'administer workspaces',
      'create test content',
      'delete any test content',
    ];
    $this
      ->drupalLogin($this
      ->drupalCreateUser($permissions));
    $this
      ->createAndActivateWorkspaceThroughUi('May 4', 'may_4');

    // Create a node in the workspace.
    $this
      ->createNodeThroughUi('A mayfly flies / In May or June', 'test');

    // Delete the workspace.
    $this
      ->drupalGet('/admin/config/workflow/workspaces/manage/may_4/delete');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $page = $this
      ->getSession()
      ->getPage();
    $page
      ->findButton('Delete')
      ->click();
    $page
      ->hasContent('The workspace May 4 has been deleted.');
  }

  /**
   * Tests the Workspaces listing UI.
   */
  public function testWorkspaceList() {
    $page = $this
      ->getSession()
      ->getPage();
    $assert_session = $this
      ->assertSession();

    // Login and create a workspace.
    $this
      ->drupalLogin($this->editor1);
    $this
      ->createWorkspaceThroughUi('Summer event', 'summer_event');

    // Check that Live is the current active workspace.
    $this
      ->drupalGet('/admin/config/workflow/workspaces');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $active_workspace_row = $page
      ->find('css', '.active-workspace');
    $this
      ->assertTrue($active_workspace_row
      ->hasClass('active-workspace--default'));
    $this
      ->assertEquals('Live', $active_workspace_row
      ->find('css', 'td:first-of-type')
      ->getText());

    // The 'Switch to Live' operation is not shown when 'Live' is the active
    // workspace.
    $assert_session
      ->linkNotExists('Switch to Live');

    // Switch to another workspace and check that it has been marked as active.
    $page
      ->clickLink('Switch to Summer event');
    $page
      ->pressButton('Confirm');
    $active_workspace_row = $page
      ->find('css', '.active-workspace');
    $this
      ->assertTrue($active_workspace_row
      ->hasClass('active-workspace--not-default'));
    $this
      ->assertEquals('Summer event', $active_workspace_row
      ->find('css', 'td:first-of-type')
      ->getText());

    // 'Live' is no longer the active workspace, so it's 'Switch to Live'
    // operation should be visible now.
    $assert_session
      ->linkExists('Switch to Live');

    // Delete any of the workspace owners and visit workspaces listing.
    $this
      ->drupalLogin($this->editor2);
    user_cancel([], $this->editor1
      ->id(), 'user_cancel_reassign');
    $user = \Drupal::service('entity_type.manager')
      ->getStorage('user')
      ->load($this->editor1
      ->id());
    $user
      ->delete();
    $this
      ->drupalGet('/admin/config/workflow/workspaces');
    $this
      ->assertSession()
      ->pageTextContains('Summer event');
    $summer_event_workspace_row = $page
      ->find('css', 'table tbody tr:nth-of-type(3)');
    $this
      ->assertEquals('N/A', $summer_event_workspace_row
      ->find('css', 'td:nth-of-type(2)')
      ->getText());
  }

  /**
   * Verifies that a workspace can be published.
   */
  public function testPublishWorkspace() {
    $this
      ->createContentType([
      'type' => 'test',
      'label' => 'Test',
    ]);
    $permissions = [
      'administer workspaces',
      'create test content',
    ];
    $this
      ->drupalLogin($this
      ->drupalCreateUser($permissions));
    $this
      ->drupalGet('/admin/config/workflow/workspaces/add');
    $this
      ->submitForm([
      'id' => 'test_workspace',
      'label' => 'Test workspace',
    ], 'Save');

    // Activate the test workspace.
    $this
      ->drupalGet('/admin/config/workflow/workspaces/manage/test_workspace/activate');
    $this
      ->submitForm([], 'Confirm');
    $this
      ->drupalGet('/admin/config/workflow/workspaces/manage/test_workspace/publish');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertSession()
      ->pageTextContains('There are no changes that can be published from Test workspace to Live.');

    // Create a node in the workspace.
    $this
      ->createNodeThroughUi('Test node', 'test');
    $this
      ->drupalGet('/admin/config/workflow/workspaces/manage/test_workspace/publish');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertSession()
      ->pageTextContains('There is 1 item that can be published from Test workspace to Live');
    $this
      ->getSession()
      ->getPage()
      ->pressButton('Publish 1 item to Live');
    $this
      ->assertSession()
      ->pageTextContains('Successful publication.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockCreationTrait::placeBlock protected function Creates a block instance based on default settings.
ContentTypeCreationTrait::createContentType protected function Creates a custom content type based on default settings.
TaxonomyTestTrait::createTaxonomyTermRevision protected function Creates a new revision for a given taxonomy term.
TaxonomyTestTrait::createTerm public function Returns a new term with random properties given a vocabulary.
TaxonomyTestTrait::createVocabulary public function Returns a new vocabulary with random properties.
WorkspaceTest::$defaultTheme protected property
WorkspaceTest::$editor1 protected property A test user.
WorkspaceTest::$editor2 protected property A test user.
WorkspaceTest::$modules protected static property
WorkspaceTest::setUp protected function
WorkspaceTest::testDeleteWorkspaceWithExistingContent public function Verifies that a workspace with existing content may be deleted.
WorkspaceTest::testPublishWorkspace public function Verifies that a workspace can be published.
WorkspaceTest::testSpecialCharacters public function Tests creating a workspace with special characters.
WorkspaceTest::testWorkspaceFieldUi public function Tests adding new fields to workspace entities.
WorkspaceTest::testWorkspaceFormRevisions public function Tests that editing a workspace creates a new revision.
WorkspaceTest::testWorkspaceList public function Tests the Workspaces listing UI.
WorkspaceTest::testWorkspaceManagePage public function Tests the manage workspace page.
WorkspaceTest::testWorkspaceOwner public function Tests changing the owner of a workspace.
WorkspaceTest::testWorkspaceToolbar public function Tests that the toolbar correctly shows the active workspace.
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 "clicking" buttons.
WorkspaceTestUtilities::createWorkspaceThroughUi protected function Creates a new Workspace through the UI.
WorkspaceTestUtilities::getOneEntityByLabel protected function Loads a single entity by its label.
WorkspaceTestUtilities::isLabelInContentOverview protected function Determine if the content list has an entity'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 "active" for subsequent requests.