Esempio n. 1
0
 /**
  * Tests the workspace filter.
  */
 public function testWorkspace()
 {
     $admin_user = $this->drupalCreateUser(['bypass node access']);
     $uid = $admin_user->id();
     $this->drupalLogin($admin_user);
     // Create two nodes on 'default' workspace.
     $node1 = $this->drupalCreateNode(['uid' => $uid]);
     $node2 = $this->drupalCreateNode(['uid' => $uid]);
     // Create a new workspace and switch to it.
     $new_workspace = Workspace::create(['machine_name' => 'new_workspace', 'label' => 'New Workspace']);
     $new_workspace->save();
     \Drupal::service('workspace.manager')->setActiveWorkspace($new_workspace);
     // Create two nodes on 'new_workspace' workspace.
     $node3 = $this->drupalCreateNode(['uid' => $uid]);
     $node4 = $this->drupalCreateNode(['uid' => $uid]);
     // Test current_workspace filter.
     $this->drupalGet('test_current_workspace', ['query' => ['workspace' => $new_workspace->id()]]);
     $this->assertNoText($node1->label());
     $this->assertNoText($node2->label());
     $this->assertText($node3->label());
     $this->assertText($node4->label());
     $this->drupalGet('test_current_workspace', ['query' => ['workspace' => 1]]);
     $this->assertText($node1->label());
     $this->assertText($node2->label());
     $this->assertNoText($node3->label());
     $this->assertNoText($node4->label());
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->workspaceManager = \Drupal::service('workspace.manager');
     $web_user = $this->drupalCreateUser(array('administer menu'));
     $this->drupalLogin($web_user);
     $this->drupalPlaceBlock('system_menu_block:main');
     $this->new_workspace = Workspace::create(['machine_name' => 'foo', 'label' => 'Foo']);
     $this->new_workspace->save();
 }
Esempio n. 3
0
 public function testSpecialCharacters()
 {
     //  Note that only lowercase characters (a-z), digits (0-9),
     // or any of the characters _, $, (, ), +, -, and / are allowed.
     $workspace1 = Workspace::create(['label' => 'Workspace 1', 'machine_name' => 'a0_$()+-/']);
     $violations1 = $workspace1->validate();
     $this->assertEqual($violations1->count(), 0, 'No violations');
     $workspace2 = Workspace::create(['label' => 'Workspace 2', 'machine_name' => 'A!"£%^&*{}#~@?']);
     $violations2 = $workspace2->validate();
     $this->assertEqual($violations2->count(), 1, 'One violation');
     $this->webUser = $this->drupalCreateUser(['administer workspaces']);
     $this->drupalLogin($this->webUser);
     $this->drupalGet('admin/structure/workspaces/add');
     $workspace3 = ['label' => 'Workspace 1', 'machine_name' => 'a0_$()+-/'];
     $this->drupalPostForm('admin/structure/workspaces/add', $workspace3, t('Save'));
     $this->drupalGet('admin/structure/workspaces');
     $this->assertText($workspace3['label'], 'Workspace found in list of workspaces');
     $workspace4 = ['label' => 'Workspace 2', 'machine_name' => 'A!"£%^&*{}#~@?'];
     $this->drupalPostForm('admin/structure/workspaces/add', $workspace4, t('Save'));
     $this->drupalGet('admin/structure/workspaces');
     $this->assertNoText($workspace4['label'], 'Workspace not found in list of workspaces');
 }
Esempio n. 4
0
 public function testBlock()
 {
     $this->drupalPlaceBlock('multiversion_workspace_block', ['region' => 'sidebar_first', 'label' => 'Workspace switcher']);
     $this->drupalGet('');
     // Confirm that the block is being displayed.
     $this->assertText('Workspace switcher', t('Block successfully being displayed on the page.'));
     $front = Url::fromRoute('<front>')->toString(TRUE)->getGeneratedUrl();
     $this->assertRaw('href="' . $front . '"', 'The id of the default workspace was displayed in the Workspace switcher block as a link.');
     $machine_name = $this->randomMachineName();
     $entity = Workspace::create(['machine_name' => $machine_name, 'label' => $machine_name]);
     $entity->save();
     $id = $entity->id();
     $node = Node::create(['type' => 'article', 'title' => 'Test article']);
     $node->save();
     $nid = $node->id();
     drupal_flush_all_caches();
     $this->drupalGet('');
     $this->assertText('Test article', 'The title of the test article was displayed on the front page.');
     $this->drupalGet("node/{$nid}");
     $this->assertText('Test article');
     $this->drupalGet('<front>');
     $url = $front . "?workspace={$id}";
     $this->assertRaw('href="' . $url . '"', 'The id of the new workspace was displayed in the Workspace switcher block as a link.');
     $this->drupalGet("/node/{$nid}", ['query' => ['workspace' => $id]]);
     $this->assertText('Page not found');
     $this->drupalGet('<front>', ['query' => ['workspace' => 'default']]);
     $this->assertText('Test article', 'The title of the test article was displayed on the front page.');
     $this->drupalGet('<front>', ['query' => ['workspace' => $id]]);
     $this->drupalGet('/node/add/article');
     $this->assertText('Create Article');
     $this->drupalGet('<front>', ['query' => ['workspace' => $id]]);
     $this->assertNoText('Test article', 'The title of the test article was not displayed on the front page after switching the workspace.');
     $entity->delete();
     drupal_flush_all_caches();
     $this->drupalGet('');
     $this->assertNoText($machine_name, 'The name of the deleted workspace was not displayed in the Workspace switcher block.');
 }
Esempio n. 5
0
 public function testEntityStorage()
 {
     // Test save and load.
     foreach ($this->entityTypes as $entity_type_id => $info) {
         // User name should be unique.
         if ($entity_type_id == 'user') {
             $info['info']['name'] = $this->randomMachineName();
         }
         $storage = $this->entityManager->getStorage($entity_type_id);
         $ids = [];
         $entity = $storage->create($info['info']);
         $return = $entity->save();
         $this->assertEqual($return, SAVED_NEW, "{$entity_type_id} was saved.");
         $ids[] = $entity->id();
         $loaded = $storage->load($ids[0]);
         $this->assertEqual($ids[0], $loaded->id(), "Single {$entity_type_id} was loaded.");
         // Load the entity with EntityRepository::loadEntityByUuid().
         $loaded = \Drupal::service('entity.repository')->loadEntityByUuid($entity_type_id, $entity->uuid());
         $this->assertEqual($ids[0], $loaded->id(), "Single {$entity_type_id} was loaded with loadEntityByUuid().");
         // Update and save a new revision.
         $entity->{$info['name']} = $this->randomMachineName();
         $entity->save();
         // For user entity type we should have three entities: anonymous, root
         // user and the new created user. For other entity types we should have
         // just the new created entity.
         $revision_id = $entity_type_id == 'user' ? 3 : 1;
         /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
         $revision = $storage->loadRevision($revision_id);
         $this->assertTrue($revision->getRevisionId() == $revision_id && !$revision->isDefaultRevision(), "Old revision of {$entity_type_id} was loaded.");
         $entity = $storage->create($info['info']);
         $entity->save();
         $ids[] = $entity->id();
         $entities = $storage->loadMultiple($ids);
         $this->assertEqual(count($entities), 2, "Multiple {$entity_type_id} was loaded.");
         // Test delete.
         if ($entity_type_id == 'user') {
             $info['info']['name'] = $this->randomMachineName();
         }
         $entity = $storage->create($info['info']);
         $entity->save();
         $id = $entity->id();
         $revision_id = $entity->getRevisionId();
         $entities = $storage->loadMultiple([$id]);
         $storage->delete($entities);
         $record = db_select($info['revision_table'], 'e')->fields('e')->condition('e.' . $info['id'], $id)->condition('e.' . $info['revision_id'], $revision_id + 1)->execute()->fetchObject();
         $this->assertEqual($record->_deleted, 1, "Deleted {$entity_type_id} is still stored but flagged as deleted");
         $entity = $storage->load($id);
         $this->assertTrue(empty($entity), "Deleted {$entity_type_id} did not load with entity_load() function.");
         $entity = $storage->loadDeleted($id);
         $this->assertTrue(!empty($entity), "Deleted {$entity_type_id} loaded with loadDeleted() method.");
         $this->assertNotEqual($revision_id, $entity->getRevisionId(), "New revision was generated when deleting {$entity_type_id}.");
         $entities = $storage->loadMultipleDeleted([$id]);
         $this->assertTrue(!empty($entities), "Deleted {$entity_type_id} loaded with loadMultipleDeleted() method.");
         // Test revisions.
         if ($entity_type_id == 'user') {
             $info['info']['name'] = $this->randomMachineName();
         }
         $entity = $storage->create($info['info']);
         $entity->save();
         $id = $entity->id();
         $revision_id = $entity->getRevisionId();
         $revision = $storage->loadRevision($revision_id);
         $this->assertEqual($revision_id, $revision->getRevisionId(), "{$entity_type_id} revision was loaded");
         $entities = $storage->loadMultiple([$id]);
         $storage->delete($entities);
         $new_revision_id = $revision_id + 1;
         $revision = $storage->loadRevision($new_revision_id);
         $this->assertTrue($revision->_deleted->value == TRUE && $revision->getRevisionId() == $new_revision_id, "Deleted {$entity_type_id} was loaded.");
         // Test exceptions.
         $entity_type = $this->entityManager->getDefinition($entity_type_id);
         $id_key = $entity_type->getKey('id');
         // Test with exception upon first save.
         if ($entity_type_id == 'user') {
             $info['info']['name'] = $this->randomMachineName();
         }
         $entity = $storage->create($info['info']);
         $uuid = $entity->uuid->value;
         try {
             // Trigger an error by setting the ID too large.
             $entity->{$id_key}->value = PHP_INT_MAX;
             $entity->save();
             $this->fail('Exception was not generated.');
         } catch (\Exception $e) {
             $first_rev = $entity->_rev->value;
             $rev_info = $this->revIndex->get("{$uuid}:{$first_rev}");
             $this->assertEqual($rev_info['status'], 'indexed', 'First revision was indexed after exception on first save.');
         }
         // Re-save the same entity with a valid ID.
         $entity->{$id_key}->value = NULL;
         $entity->save();
         $second_rev = $entity->_rev->value;
         $this->assertEqual($first_rev, $second_rev, 'New revision was not generated after first re-save.');
         $rev_info = $this->revIndex->get("{$uuid}:{$first_rev}");
         $this->assertEqual($rev_info['status'], 'available', 'First revision is available after first re-save.');
         $default_branch = $this->revTree->getDefaultBranch($uuid);
         $expected_default_branch = [$first_rev => 'available'];
         $this->assertEqual($default_branch, $expected_default_branch, 'Default branch was built after exception on first save followed by re-save.');
         // Test with exception upon second save.
         if ($entity_type_id == 'user') {
             $info['info']['name'] = $this->randomMachineName();
         }
         $entity = $storage->create($info['info']);
         $uuid = $entity->uuid->value;
         $entity->save();
         $first_id = $entity->id();
         $first_rev = $entity->_rev->value;
         try {
             // Temporary solution.
             // @todo: {@link https://www.drupal.org/node/2597516 Remove now that
             // https://www.drupal.org/node/2453153 is fixed.}
             $entity->original = clone $entity;
             // Trigger an error by setting the ID too large.
             $entity->{$id_key}->value = PHP_INT_MAX;
             $entity->save();
             $this->fail('Exception was not generated.');
         } catch (\Exception $e) {
             $second_rev = $entity->_rev->value;
             $rev_info = $this->revIndex->get("{$uuid}:{$second_rev}");
             $this->assertEqual($rev_info['status'], 'indexed', 'Second revision was indexed after exception on second save.');
         }
         // Re-save the same entity with a valid ID.
         $entity->{$id_key}->value = $first_id;
         $entity->save();
         $third_rev = $entity->_rev->value;
         $this->assertEqual($second_rev, $third_rev, 'New revision was not generated after second re-save.');
         $rev_info = $this->revIndex->get("{$uuid}:{$second_rev}");
         $this->assertEqual($rev_info['status'], 'available', 'Third revision is available after second re-save.');
         $default_branch = $this->revTree->getDefaultBranch($uuid);
         $expected_default_branch = [$first_rev => 'available', $second_rev => 'available'];
         $this->assertEqual($default_branch, $expected_default_branch, 'Default branch was built after exception on second save followed by re-save.');
         // Test workspace.
         if ($entity_type_id == 'user') {
             $info['info']['name'] = $this->randomMachineName();
         }
         $entity = $storage->create($info['info']);
         $entity->save();
         $entity_id = $entity->id();
         $this->assertEqual($entity->workspace->target_id, 1, "The workspace reference was saved for {$entity_type_id}.");
         $record = db_select($info['data_table'], 'e')->fields('e')->condition('e.' . $info['id'], $entity->id())->condition('e.' . $info['revision_id'], $entity->getRevisionId())->execute()->fetchObject();
         $this->assertEqual($record->workspace, 1, "The workspace reference was stored for saved {$entity_type_id}.");
         $entity = $storage->load($entity_id);
         $this->assertEqual($entity->workspace->target_id, 1, "The workspace reference is retained for loaded {$entity_type_id}.");
         $record = db_select($info['data_table'], 'e')->fields('e')->condition('e.' . $info['id'], $entity->id())->condition('e.' . $info['revision_id'], $entity->getRevisionId())->execute()->fetchObject();
         $this->assertEqual($record->workspace, 1, "The workspace reference was stored for loaded {$entity_type_id}.");
         $entities = $storage->loadMultiple([$entity_id]);
         $storage->delete($entities);
         $entity = $storage->loadDeleted($entity_id);
         $this->assertEqual($entity->workspace->target_id, 1, "The workspace reference is retained for deleted {$entity_type_id}.");
         $record = db_select($info['data_table'], 'e')->fields('e')->condition('e.' . $info['id'], $entity->id())->condition('e.' . $info['revision_id'], $entity->getRevisionId())->execute()->fetchObject();
         $this->assertEqual($record->workspace, 1, "The workspace reference was stored for deleted {$entity_type_id}.");
     }
     // Test workspace when switching the workspace.
     // Create a new workspace and switch to it.
     $workspace = Workspace::create(['id' => $this->randomMachineName()]);
     $this->workspaceManager->setActiveWorkspace($workspace);
     foreach ($this->entityTypes as $entity_type_id => $info) {
         $storage = $this->entityManager->getStorage($entity_type_id);
         $entity = $storage->create($info['info']);
         $entity->save();
         $this->assertEqual($entity->workspace->target_id, $workspace->id(), "{$entity_type_id} was saved in new workspace.");
     }
     $uuids = array();
     $ids = array();
     foreach ($this->entityTypes as $entity_type_id => $info) {
         $storage = $this->entityManager->getStorage($entity_type_id);
         if ($entity_type_id == 'user') {
             $info['info']['name'] = $this->randomMachineName();
         }
         $entity = $storage->create($info['info']);
         $entity->save();
         $uuids[$entity_type_id] = $entity->uuid();
         $ids[$entity_type_id] = $entity->id();
         $entity = $storage->load($ids[$entity_type_id]);
         $this->assertTrue(!empty($entity), "{$entity_type_id} was loaded in the workspace it belongs to.");
         $entity = $this->entityManager->loadEntityByUuid($entity_type_id, $uuids[$entity_type_id]);
         $this->assertTrue(!empty($entity), "{$entity_type_id} was loaded by UUID in the workspace it belongs to.");
     }
     $this->multiversionManager->setActiveWorkspaceId(1);
     foreach ($this->entityTypes as $entity_type_id => $info) {
         $storage = $this->entityManager->getStorage($entity_type_id);
         $entity = $storage->load($ids[$entity_type_id]);
         if ($entity_type_id == 'user') {
             $this->assertFalse(empty($entity), "{$entity_type_id} was loaded in a workspace it does not belongs to.");
         } else {
             $this->assertTrue(empty($entity), "{$entity_type_id} was not loaded in a workspace it does not belongs to.");
         }
         $entity = $this->entityManager->loadEntityByUuid($entity_type_id, $uuids[$entity_type_id]);
         if ($entity_type_id == 'user') {
             $this->assertFalse(empty($entity), "{$entity_type_id} was loaded by UUID in a workspace it does not belong to.");
         } else {
             $this->assertTrue(empty($entity), "{$entity_type_id} was not loaded by UUID in a workspace it does not belong to.");
         }
     }
 }