View source
<?php
namespace Drupal\Tests\multiversion\Functional;
use Drupal\multiversion\Entity\Workspace;
use Drupal\multiversion\Entity\WorkspaceInterface;
class WorkspaceTest extends MultiversionFunctionalTestBase {
protected $strictConfigSchema = FALSE;
protected static $modules = [
'multiversion',
'key_value',
];
public function testOperations() {
$default = Workspace::load(1);
$this
->assertTrue(!empty($default), 'Default workspace was created when installing Multiversion module.');
$machine_name = $this
->randomMachineName();
$entity = Workspace::create([
'machine_name' => $machine_name,
'label' => $machine_name,
'type' => 'basic',
]);
$this
->assertTrue($entity instanceof WorkspaceInterface, 'Workspace entity was created.');
$entity
->save();
$this
->assertEqual($machine_name, $entity
->get('machine_name')->value, 'Workspace entity was saved.');
$entity = Workspace::load($entity
->id());
$this
->assertEqual($machine_name, $entity
->get('machine_name')->value, 'Workspace entity was loaded by ID.');
$this
->assertEqual($machine_name, $entity
->label(), 'Label method returns the workspace name.');
$created = $entity
->getStartTime();
$this
->assertNotNull($created, "The value for 'created' field is not null.");
$new_created_time = microtime(TRUE) * 1000000;
$entity
->setCreatedTime((int) $new_created_time);
$this
->assertEqual($new_created_time, $entity
->getStartTime(), "Correct value for 'created' field.");
$workspace1 = Workspace::create([
'label' => 'Workspace 1',
'machine_name' => 'a0_$()+-/',
'type' => 'basic',
]);
$violations1 = $workspace1
->validate();
$this
->assertEqual($violations1
->count(), 0, 'No violations');
$workspace2 = Workspace::create([
'label' => 'Workspace 2',
'machine_name' => 'A!"£%^&*{}#~@?',
'type' => 'basic',
]);
$violations2 = $workspace2
->validate();
$this
->assertEqual($violations2
->count(), 1, 'One violation');
}
public function testActiveWorkspace() {
$live = $this->workspaceManager
->getActiveWorkspace();
$this
->assertEqual('live', $live
->getMachineName());
$test1 = Workspace::create([
'machine_name' => 'test1',
'label' => 'test1',
'type' => 'basic',
]);
$test1
->save();
$test2 = Workspace::create([
'machine_name' => 'test2',
'label' => 'test2',
'type' => 'basic',
]);
$test2
->save();
$user1 = $this
->drupalCreateUser([
'administer workspaces',
]);
$user2 = $this
->drupalCreateUser([
'administer workspaces',
]);
$this
->drupalLogin($user1);
$this
->assertEqual('live', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
$this->workspaceManager
->setActiveWorkspace($test1);
$this
->assertEqual('test1', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
$this
->drupalLogout();
$this
->assertEqual('live', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
$this
->drupalLogin($user1);
$this
->assertEqual('test1', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
$this
->drupalLogout();
$this
->drupalLogin($user2);
$this->workspaceManager
->setActiveWorkspace($test2);
$this
->assertEqual('test2', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
$this
->drupalLogout();
$this
->drupalLogin($user1);
$this
->assertEqual('test1', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
}
public function testDeleteWorkspaceActiveForUser() {
$cats = Workspace::create([
'label' => 'Cats',
'machine_name' => 'cats',
'type' => 'basic',
]);
$cats
->save();
$dogs = Workspace::create([
'label' => 'Dogs',
'machine_name' => 'dogs',
'type' => 'basic',
]);
$dogs
->save();
$dogs_id = $dogs
->id();
$alina = $this
->drupalCreateUser([
'administer workspaces',
]);
$john = $this
->drupalCreateUser([
'administer workspaces',
]);
$this
->drupalLogin($alina);
$this->workspaceManager
->setActiveWorkspace($cats);
$this
->assertEqual('cats', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
$this
->drupalLogin($john);
$this->workspaceManager
->setActiveWorkspace($dogs);
$this
->assertEqual('dogs', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
$this
->drupalLogin($alina);
$this
->assertEqual('cats', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
$dogs
->delete();
\Drupal::service('cron')
->run();
$this
->assertEmpty(Workspace::load($dogs_id));
$this
->drupalLogin($john);
$this
->assertEqual('live', $this->workspaceManager
->getActiveWorkspace()
->getMachineName());
}
}