You are here

public function ReplicatorAliasTest::testReplicationAlias in Workspace 8

Verifies that a user can edit anything in a workspace with a specific perm.

File

tests/src/Functional/ReplicatorAliasTest.php, line 100

Class

ReplicatorAliasTest
Test the workspace entity with EventSubscriber alias change.

Namespace

Drupal\Tests\workspace\Functional

Code

public function testReplicationAlias() {
  $this
    ->createPattern('node', '[node:title]');
  $live = $this
    ->getOneEntityByLabel('workspace', 'Live');
  $this
    ->drupalGet('/node/add/test');
  $session = $this
    ->getSession();
  $this
    ->assertEquals(200, $session
    ->getStatusCode());
  $page = $session
    ->getPage();
  $page
    ->fillField('Title', 'Test node');
  $page
    ->fillField('Provide a menu link', 1);
  $page
    ->fillField('Menu link title', 'Test node link');
  $page
    ->findButton(t('Save'))
    ->click();
  $page = $session
    ->getPage();
  $page
    ->hasContent("Test node has been created");
  $test_node_live = $this
    ->getOneEntityByLabel('node', 'Test node');
  $this->pathAutoGenerator
    ->createEntityAlias($test_node_live, 'insert');
  $test_node_live_alias = $this->aliasManager
    ->getAliasByPath('/node/' . $test_node_live
    ->id());
  $this
    ->assertEquals($live
    ->id(), $test_node_live
    ->get('workspace')->entity
    ->id());
  $this
    ->drupalGet('/admin/content');
  $session = $this
    ->getSession();
  $this
    ->assertEquals(200, $session
    ->getStatusCode());
  $page = $session
    ->getPage();
  $page
    ->hasContent($test_node_live
    ->label());
  $menu_link_live = $this
    ->getOneEntityByLabel('menu_link_content', 'Test node link');
  $this
    ->assertEquals($live
    ->id(), $menu_link_live
    ->get('workspace')->entity
    ->id());
  $this
    ->drupalGet('/admin/structure/menu/manage/main');
  $session = $this
    ->getSession();
  $this
    ->assertEquals(200, $session
    ->getStatusCode());
  $page = $session
    ->getPage();
  $page
    ->hasContent('Test node link');
  $target = $this
    ->createWorkspaceThroughUI('Target', 'target');

  /** @var \Drupal\workspace\ReplicatorManager $rm */
  $rm = \Drupal::service('workspace.replicator_manager');
  $task = new ReplicationTask();
  $rm
    ->replicate($this
    ->getPointerToWorkspace($live), $this
    ->getPointerToWorkspace($target), $task);
  \Drupal::service('cron')
    ->run();
  $replication_log_id = $this
    ->getPointerToWorkspace($live)
    ->generateReplicationId($this
    ->getPointerToWorkspace($target), $task);
  $replication_logs = $this->entityTypeManager
    ->getStorage('replication_log')
    ->getQuery()
    ->allRevisions()
    ->condition('uuid', $replication_log_id)
    ->execute();
  $this
    ->assertEquals(2, count($replication_logs));
  $i = 1;
  foreach ($replication_logs as $revision_id => $id) {
    $this
      ->assertEquals(2, $id);
    $this
      ->assertEquals($i * 2, $revision_id);

    /** @var \Drupal\replication\Entity\ReplicationLogInterface $revision */
    $revision = $this->entityTypeManager
      ->getStorage('replication_log')
      ->loadRevision($revision_id);
    $this
      ->assertTrue($revision->ok->value);
    if ($i == 1) {
      $this
        ->assertNull($revision
        ->getHistory()[0]['docs_written']);
    }
    else {
      $this
        ->assertEquals(2, $revision
        ->getHistory()[0]['docs_written']);
    }
    $i++;
  }
  $this
    ->switchToWorkspace($target);
  $test_node_target = $this
    ->getOneEntityByLabel('node', 'Test node');
  $this
    ->assertEquals($target
    ->id(), $test_node_target
    ->get('workspace')->entity
    ->id());
  $this
    ->drupalGet('/admin/content');
  $session = $this
    ->getSession();
  $this
    ->assertEquals(200, $session
    ->getStatusCode());
  $page = $session
    ->getPage();
  $page
    ->hasContent($test_node_target
    ->label());
  $menu_link_target = $this
    ->getOneEntityByLabel('menu_link_content', 'Test node link');
  $this
    ->assertEquals($target
    ->id(), $menu_link_target
    ->get('workspace')->entity
    ->id());
  $this
    ->drupalGet('/admin/structure/menu/manage/main');
  $session = $this
    ->getSession();
  $this
    ->assertEquals(200, $session
    ->getStatusCode());
  $page = $session
    ->getPage();
  $page
    ->hasContent('Test node link');
  $this
    ->assertEntityAlias($test_node_target, $test_node_live_alias);
}