switchToWorkspace example

public function testWorkspaceManagePage() {
    $this->drupalLogin($this->rootUser);
    $this->setupWorkspaceSwitcherBlock();
    $assert_session = $this->assertSession();

    $this->drupalCreateContentType(['type' => 'test', 'label' => 'Test']);
    $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');

    
'bypass entity access own workspace',
    ];

    $this->createContentType(['type' => 'test', 'label' => 'Test']);
    $this->setupWorkspaceSwitcherBlock();

    $ditka = $this->drupalCreateUser(array_merge($permissions['create test content']));

    // Login as a limited-access user and create a workspace.     $this->drupalLogin($ditka);
    $bears = $this->createWorkspaceThroughUi('Bears', 'bears');
    $this->switchToWorkspace($bears);

    // Now create a node in the Bears workspace, as the owner of that workspace.     $ditka_bears_node = $this->createNodeThroughUi('Ditka Bears node', 'test');
    $ditka_bears_node_id = $ditka_bears_node->id();

    // Editing both nodes should be possible.     $this->drupalGet('/node/' . $ditka_bears_node_id . '/edit');
    $this->assertSession()->statusCodeEquals(200);

    // Create a new user that should be able to edit anything in the Bears     // workspace.

  public function testPathAliases() {
    // Create a published node in Live, without an alias.     $node = $this->drupalCreateNode([
      'type' => 'article',
      'status' => TRUE,
    ]);

    // Switch to Stage and create an alias for the node.     $stage = Workspace::load('stage');
    $this->switchToWorkspace($stage);

    $edit = [
      'path[0][alias]' => '/' . $this->randomMachineName(),
    ];
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Save');

    // Check that the node can be accessed in Stage with the given alias.     $path = $edit['path[0][alias]'];
    $this->assertAccessiblePaths([$path]);

    

      ],
    ]);
    $test_scenarios['push_stage_to_live'] = $revision_state;
    $expected_workspace_association['push_stage_to_live'] = ['stage' => []];

    // Check the initial state after the module was installed.     $this->assertWorkspaceStatus($test_scenarios['initial_state'], 'node');
    $this->assertWorkspaceAssociation($expected_workspace_association['initial_state'], 'node');

    // Unpublish node 1 in 'stage'.     $this->switchToWorkspace('stage');
    $node = $this->entityTypeManager->getStorage('node')->load(1);
    $node->setTitle('stage - 1 - r3 - unpublished');
    $node->setUnpublished();
    $node->save();
    $this->assertWorkspaceStatus($test_scenarios['unpublish_node_1_in_stage'], 'node');
    $this->assertWorkspaceAssociation($expected_workspace_association['unpublish_node_1_in_stage'], 'node');

    // Publish node 2 in 'stage'.     $this->switchToWorkspace('stage');
    $node = $this->entityTypeManager->getStorage('node')->load(2);
    $node->setTitle('stage - 2 - r4 - published');
    
$default_title = 'default';
    $default_link = '#live';
    $menu_link_content = MenuLinkContent::create([
      'title' => $default_title,
      'menu_name' => 'main',
      'link' => [['uri' => 'internal:/' . $default_link]],
    ]);
    $menu_link_content->save();

    $pending_title = 'pending';
    $pending_link = 'http://example.com';
    $this->switchToWorkspace($stage);
    $menu_link_content->set('title', $pending_title);
    $menu_link_content->set('link', [['uri' => $pending_link]]);
    $menu_link_content->save();

    $this->drupalGet('');
    $assert_session = $this->assertSession();
    $assert_session->linkExists($pending_title);
    $assert_session->linkByHrefExists($pending_link);

    // Add a new menu link in the Stage workspace.     $menu_link_content = MenuLinkContent::create([
      
// Enable moderation on Article node type.     $this->createContentTypeFromUi('Article', 'article', TRUE);

    $this->setupWorkspaceSwitcherBlock();
  }

  /** * Tests moderating nodes in a workspace. */
  public function testModerationInWorkspace() {
    $stage = Workspace::load('stage');
    $this->switchToWorkspace($stage);

    // Create two nodes, a published and a draft one.     $this->drupalGet('node/add/article');
    $this->submitForm([
      'title[0][value]' => 'First article - published',
      'moderation_state[0][state]' => 'published',
    ], 'Save');
    $this->drupalGet('node/add/article');
    $this->submitForm([
      'title[0][value]' => 'Second article - draft',
      'moderation_state[0][state]' => 'draft',
    ],
/** * Tests the revisions tracked by a workspace. * * @covers ::getTrackedEntities * @covers ::getAssociatedRevisions */
  public function testWorkspaceAssociation() {
    $this->createNode(['title' => 'Test article 1 - live - unpublished', 'type' => 'article', 'status' => 0]);
    $this->createNode(['title' => 'Test article 2 - live - published', 'type' => 'article']);

    // Edit one of the existing nodes in 'stage'.     $this->switchToWorkspace('stage');
    $node = $this->entityTypeManager->getStorage('node')->load(1);
    $node->setTitle('Test article 1 - stage - published');
    $node->setPublished();
    // This creates rev. 3.     $node->save();

    // Generate content with the following structure:     // Stage:     // - Test article 3 - stage - unpublished (rev. 4)     // - Test article 4 - stage - published (rev. 5 and 6)     $this->createNode(['title' => 'Test article 3 - stage - unpublished', 'type' => 'article', 'status' => 0]);
    
'supported_reference' => [
        'entity' => EntityTest::create([]),
      ],
    ]);
    $this->assertCount(0, $entity->validate());
  }

  /** * @covers ::validate */
  public function testNewEntitiesForbiddenInNonDefaultWorkspace() {
    $this->switchToWorkspace('stage');
    $entity = EntityTestMulRevPub::create([
      'unsupported_reference' => [
        'entity' => EntityTest::create([]),
      ],
      'supported_reference' => [
        'entity' => EntityTestMulRevPub::create([]),
      ],
    ]);
    $violations = $entity->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals('Test entity entities can only be created in the default workspace.', $violations[0]->getMessage());
  }
protected $revEntityTypeId = 'entity_test_revpub';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->installSchema('system', ['sequences']);

    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');
  }

  /** * Tests that the 'workspace' entity type can not be moderated. * * @see \Drupal\workspaces\EntityTypeInfo::entityTypeAlter() */
  public function testWorkspaceEntityTypeModeration() {
    /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
    $moderation_info = \Drupal::service('content_moderation.moderation_information');
    $entity_type = \Drupal::entityTypeManager()->getDefinition('workspace');
    
$this->setupWorkspaceSwitcherBlock();

    $mayer = $this->drupalCreateUser($permissions);
    $this->drupalLogin($mayer);
  }

  /** * Tests switching workspace via the switcher block and admin page. */
  public function testSwitchingWorkspaces() {
    $vultures = $this->createWorkspaceThroughUi('Vultures', 'vultures');
    $this->switchToWorkspace($vultures);

    $gravity = $this->createWorkspaceThroughUi('Gravity', 'gravity');

    $this->drupalGet('/admin/config/workflow/workspaces/manage/' . $gravity->id() . '/activate');

    $this->assertSession()->statusCodeEquals(200);
    $page = $this->getSession()->getPage();
    $page->findButton('Confirm')->click();

    // Check that WorkspaceCacheContext provides the cache context used to     // support its functionality.
$test_node = $this->createNodeThroughUi('Test node', 'test');

    // Check that the user can edit the node.     $page = $this->getSession()->getPage();
    $page->hasField('title[0][value]');

    // Create two workspaces.     $vultures = $this->createWorkspaceThroughUi('Vultures', 'vultures');
    $gravity = $this->createWorkspaceThroughUi('Gravity', 'gravity');

    // Edit the node in workspace 'vultures'.     $this->switchToWorkspace($vultures);
    $this->drupalGet('/node/' . $test_node->id() . '/edit');
    $page = $this->getSession()->getPage();
    $page->fillField('Title', 'Test node - override');
    $page->findButton('Save')->click();

    // Check that the user can still edit the node in the same workspace.     $this->drupalGet('/node/' . $test_node->id() . '/edit');
    $page = $this->getSession()->getPage();
    $this->assertTrue($page->hasField('title[0][value]'));

    // Switch to a different workspace and check that the user can not edit the
    // Local 2:     // - Test article 2 - stage     // - Test article 3 - dev     //     // Note that the contents of each workspace are inherited automatically in     // each of its descendants.     $this->createNode(['title' => 'Test article 1 - live', 'type' => 'article']);

    // This creates revisions 2 and 3. Revision 2 is an unpublished default     // revision (which is also available in Live), and revision 3 is a published     // pending revision that is available in Stage and all its descendants.     $this->switchToWorkspace('stage');
    $this->createNode(['title' => 'Test article 2 - stage', 'type' => 'article']);

    $expected_workspace_association = [
      'stage' => [3],
      'dev' => [3],
      'local_1' => [3],
      'local_2' => [3],
      'qa' => [],
    ];
    $this->assertWorkspaceAssociation($expected_workspace_association, 'node');

    
Home | Imprint | This part of the site doesn't use cookies.