switchToLive example

public function testCliPublishing() {
    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');

    // Add a workspace-specific revision to a pre-existing node.     $node = $this->entityTypeManager->getStorage('node')->load(2);
    $node->title->value = 'stage - 2 - r3 - published';
    $node->save();

    // Switch to an anonymous user account and the 'Live' workspace.     \Drupal::service('account_switcher')->switchTo(new AnonymousUserSession());
    \Drupal::service('workspaces.manager')->switchToLive();

    // Publish the workspace as anonymous, simulating a CLI request.     $this->workspaces['stage']->publish();
  }

  /** * Tests entity query overrides without any conditions. */
  public function testEntityQueryWithoutConditions() {
    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');

    
$page = $this->getSession()->getPage();
    $this->assertFalse($page->hasField('title[0][value]'));
    $page->hasContent('The content is being edited in the Vultures workspace. As a result, your changes cannot be saved.');

    // Check that the node fails validation for API calls.     $violations = $test_node->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals('The content is being edited in the Vultures workspace. As a result, your changes cannot be saved.', $violations->get(0)->getMessage());

    // Switch to the Live version of the site and check that the user still can     // not edit the node.     $this->switchToLive();
    $this->drupalGet('/node/' . $test_node->id() . '/edit');
    $page = $this->getSession()->getPage();
    $this->assertFalse($page->hasField('title[0][value]'));
    $page->hasContent('The content is being edited in the Vultures workspace. As a result, your changes cannot be saved.');

    // Check that the node fails validation for API calls.     $violations = $test_node->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals('The content is being edited in the Vultures workspace. As a result, your changes cannot be saved.', $violations->get(0)->getMessage());

    // Publish the changes from the 'Vultures' workspace and check that the node
'title[0][value]' => 'Second article - draft',
      'moderation_state[0][state]' => 'draft',
    ], 'Save');

    $first_article = $this->drupalGetNodeByTitle('First article - published', TRUE);
    $this->assertEquals('published', $first_article->moderation_state->value);

    $second_article = $this->drupalGetNodeByTitle('Second article - draft', TRUE);
    $this->assertEquals('draft', $second_article->moderation_state->value);

    // Check that neither of them are visible in Live.     $this->switchToLive();
    $this->drupalGet('<front>');
    $this->assertSession()->pageTextNotContains('First article');
    $this->assertSession()->pageTextNotContains('Second article');

    // Switch back to Stage.     $this->switchToWorkspace($stage);

    // Take the first node through various moderation states.     $this->drupalGet('/node/1/edit');
    $this->assertEquals('Current state Published', $this->cssSelect('#edit-moderation-state-0-current')[0]->getText());

    
/** * {@inheritdoc} */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage$entities);

    /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
    $workspace_manager = \Drupal::service('workspaces.manager');
    // Disable the currently active workspace if it has been deleted.     if ($workspace_manager->hasActiveWorkspace()
      && in_array($workspace_manager->getActiveWorkspace()->id()array_keys($entities), TRUE)) {
      $workspace_manager->switchToLive();
    }

    // Ensure that workspace batch purging does not happen inside a workspace.     $workspace_manager->executeOutsideWorkspace(function D) use ($workspace_manager$entities) {
      // Add the IDs of the deleted workspaces to the list of workspaces that will       // be purged on cron.       $state = \Drupal::state();
      $deleted_workspace_ids = $state->get('workspace.deleted', []);
      $deleted_workspace_ids += array_combine(array_keys($entities)array_keys($entities));
      $state->set('workspace.deleted', $deleted_workspace_ids);

      

  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 */
/** * {@inheritdoc} */
  public function getCancelUrl() {
    return new Url('<current>');
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->workspaceManager->switchToLive();
    $this->messenger()->addMessage($this->t('You are now viewing the live version of the site.'));
  }

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

    // Check that the 'preload-paths' cache includes the active workspace ID in     // the cache key.     $this->assertNotEmpty(\Drupal::cache('data')->get('preload-paths:stage:/node/1'));
    $this->assertFalse(\Drupal::cache('data')->get('preload-paths:/node/1'));

    // Check that the alias can not be accessed in Live.     $this->switchToLive();
    $this->assertNotAccessiblePaths([$path]);
    $this->assertFalse(\Drupal::cache('data')->get('preload-paths:/node/1'));

    // Publish the workspace and check that the alias can be accessed in Live.     $stage->publish();
    $this->assertAccessiblePaths([$path]);
    $this->assertNotEmpty(\Drupal::cache('data')->get('preload-paths:/node/1'));
  }

  /** * Tests path aliases with workspaces and user switching. */
'menu_name' => 'main',
      'link' => [['uri' => 'internal:/#stage']],
    ]);
    $menu_link_content->save();

    $this->drupalGet('');
    $assert_session->linkExists('stage link');
    $assert_session->linkByHrefExists('#stage');

    // Switch back to the Live workspace and check that the menu link has the     // default values.     $this->switchToLive();
    $this->drupalGet('');
    $assert_session->linkExists($default_title);
    $assert_session->linkByHrefExists($default_link);
    $assert_session->linkNotExists($pending_title);
    $assert_session->linkByHrefNotExists($pending_link);
    $assert_session->linkNotExists('stage link');
    $assert_session->linkByHrefNotExists('#stage');

    // Publish the workspace and check that the menu link has been updated.     $stage->publish();
    $this->drupalGet('');
    
$this->messenger->addMessage($this->t('%workspace_label is now the active workspace.', ['%workspace_label' => $workspace->label()]));
    }
    catch (WorkspaceAccessException $e) {
      $this->messenger->addError($this->t('You do not have access to activate the %workspace_label workspace.', ['%workspace_label' => $workspace->label()]));
    }
  }

  /** * Submit handler for switching to the live version of the site. */
  public function submitSwitchToLive(array &$form, FormStateInterface $form_state) {
    $this->workspaceManager->switchToLive();
    $this->messenger->addMessage($this->t('You are now viewing the live version of the site.'));
  }

}
// 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.     $this->switchToLive();
    $lombardi = $this->drupalCreateUser(array_merge($permissions['view any workspace']));
    $this->drupalLogin($lombardi);
    $this->switchToWorkspace($bears);

    // Editor 2 has the bypass permission but does not own the workspace and so,     // should not be able to create and edit any node.     $this->drupalGet('/node/' . $ditka_bears_node_id . '/edit');
    $this->assertSession()->statusCodeEquals(403);
  }

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