You are here

public function WorkspaceTest::testOperations in Multiversion 8

File

tests/src/Functional/WorkspaceTest.php, line 19

Class

WorkspaceTest
Test the workspace entity.

Namespace

Drupal\Tests\multiversion\Functional

Code

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.");

  //  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_$()+-/',
    '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');
}