You are here

public function RevisionTreeIndexTest::testWithoutDelete in Multiversion 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/RevisionTreeIndexTest.php \Drupal\Tests\multiversion\Functional\RevisionTreeIndexTest::testWithoutDelete()

File

tests/src/Functional/RevisionTreeIndexTest.php, line 40

Class

RevisionTreeIndexTest
Test the methods on the RevisionTreeIndex class.

Namespace

Drupal\Tests\multiversion\Functional

Code

public function testWithoutDelete() {
  $storage = $this->entityTypeManager
    ->getStorage('entity_test');
  $entity = $storage
    ->create();
  $uuid = $entity
    ->uuid();

  // Create a conflict scenario to fully test the parsing.
  // Initial revision.
  $entity
    ->save();
  $revs[] = $entity->_rev->value;
  $entity
    ->save();
  $revs[] = $entity->_rev->value;
  $entity
    ->save();
  $revs[] = $leaf_one = $entity->_rev->value;
  $entity = $storage
    ->load(1);
  $this
    ->assertEqual($entity
    ->getRevisionId(), 3, 'Default revision has been set correctly.');

  // Create a new branch from the second revision.
  $entity = $storage
    ->loadRevision(2);
  $entity
    ->save();
  $revs[] = $leaf_two = $entity->_rev->value;

  // We now have two leafs at the tip of the tree.
  $leafs = [
    $leaf_one,
    $leaf_two,
  ];
  sort($leafs);
  $expected_leaf = array_pop($leafs);
  $entity = $storage
    ->load(1);
  $this
    ->assertEqual($entity->_rev->value, $expected_leaf, 'The correct revision won while having two open revisions.');

  // Continue the last branch.
  $entity = $storage
    ->loadRevision(4);
  $entity
    ->save();
  $revs[] = $entity->_rev->value;
  $entity = $storage
    ->load(1);
  $this
    ->assertEqual($entity
    ->getRevisionId(), 5, 'Default revision has been set correctly.');

  // Add 10 more revisions to test IDs with double figures.
  for ($x = 0; $x <= 10; $x++) {
    $entity
      ->save();
    $revs[] = $entity->_rev->value;
  }

  // Create a new branch based on the first revision.
  $entity = $storage
    ->loadRevision(1);
  $entity
    ->save();
  $revs[] = $entity->_rev->value;
  $entity = $storage
    ->load(1);
  $this
    ->assertEqual($entity
    ->getRevisionId(), 16, 'Default revision has been set correctly.');
  $expected_tree = [
    [
      '#type' => 'rev',
      '#uuid' => $uuid,
      '#rev' => $revs[0],
      '#rev_info' => [
        'status' => 'available',
        'default' => FALSE,
        'open_rev' => FALSE,
        'conflict' => FALSE,
      ],
      'children' => [
        [
          '#type' => 'rev',
          '#uuid' => $uuid,
          '#rev' => $revs[1],
          '#rev_info' => [
            'status' => 'available',
            'default' => FALSE,
            'open_rev' => FALSE,
            'conflict' => FALSE,
          ],
          'children' => [
            [
              '#type' => 'rev',
              '#uuid' => $uuid,
              '#rev' => $revs[2],
              '#rev_info' => [
                'status' => 'available',
                'default' => FALSE,
                'open_rev' => TRUE,
                'conflict' => TRUE,
              ],
              'children' => [],
            ],
            [
              '#type' => 'rev',
              '#uuid' => $uuid,
              '#rev' => $revs[3],
              '#rev_info' => [
                'status' => 'available',
                'default' => FALSE,
                'open_rev' => FALSE,
                'conflict' => FALSE,
              ],
              'children' => [
                [
                  '#type' => 'rev',
                  '#uuid' => $uuid,
                  '#rev' => $revs[4],
                  '#rev_info' => [
                    'status' => 'available',
                    'default' => FALSE,
                    'open_rev' => FALSE,
                    'conflict' => FALSE,
                  ],
                  'children' => [
                    [
                      '#type' => 'rev',
                      '#uuid' => $uuid,
                      '#rev' => $revs[5],
                      '#rev_info' => [
                        'status' => 'available',
                        'default' => FALSE,
                        'open_rev' => FALSE,
                        'conflict' => FALSE,
                      ],
                      'children' => [
                        [
                          '#type' => 'rev',
                          '#uuid' => $uuid,
                          '#rev' => $revs[6],
                          '#rev_info' => [
                            'status' => 'available',
                            'default' => FALSE,
                            'open_rev' => FALSE,
                            'conflict' => FALSE,
                          ],
                          'children' => [
                            [
                              '#type' => 'rev',
                              '#uuid' => $uuid,
                              '#rev' => $revs[7],
                              '#rev_info' => [
                                'status' => 'available',
                                'default' => FALSE,
                                'open_rev' => FALSE,
                                'conflict' => FALSE,
                              ],
                              'children' => [
                                [
                                  '#type' => 'rev',
                                  '#uuid' => $uuid,
                                  '#rev' => $revs[8],
                                  '#rev_info' => [
                                    'status' => 'available',
                                    'default' => FALSE,
                                    'open_rev' => FALSE,
                                    'conflict' => FALSE,
                                  ],
                                  'children' => [
                                    [
                                      '#type' => 'rev',
                                      '#uuid' => $uuid,
                                      '#rev' => $revs[9],
                                      '#rev_info' => [
                                        'status' => 'available',
                                        'default' => FALSE,
                                        'open_rev' => FALSE,
                                        'conflict' => FALSE,
                                      ],
                                      'children' => [
                                        [
                                          '#type' => 'rev',
                                          '#uuid' => $uuid,
                                          '#rev' => $revs[10],
                                          '#rev_info' => [
                                            'status' => 'available',
                                            'default' => FALSE,
                                            'open_rev' => FALSE,
                                            'conflict' => FALSE,
                                          ],
                                          'children' => [
                                            [
                                              '#type' => 'rev',
                                              '#uuid' => $uuid,
                                              '#rev' => $revs[11],
                                              '#rev_info' => [
                                                'status' => 'available',
                                                'default' => FALSE,
                                                'open_rev' => FALSE,
                                                'conflict' => FALSE,
                                              ],
                                              'children' => [
                                                [
                                                  '#type' => 'rev',
                                                  '#uuid' => $uuid,
                                                  '#rev' => $revs[12],
                                                  '#rev_info' => [
                                                    'status' => 'available',
                                                    'default' => FALSE,
                                                    'open_rev' => FALSE,
                                                    'conflict' => FALSE,
                                                  ],
                                                  'children' => [
                                                    [
                                                      '#type' => 'rev',
                                                      '#uuid' => $uuid,
                                                      '#rev' => $revs[13],
                                                      '#rev_info' => [
                                                        'status' => 'available',
                                                        'default' => FALSE,
                                                        'open_rev' => FALSE,
                                                        'conflict' => FALSE,
                                                      ],
                                                      'children' => [
                                                        [
                                                          '#type' => 'rev',
                                                          '#uuid' => $uuid,
                                                          '#rev' => $revs[14],
                                                          '#rev_info' => [
                                                            'status' => 'available',
                                                            'default' => FALSE,
                                                            'open_rev' => FALSE,
                                                            'conflict' => FALSE,
                                                          ],
                                                          'children' => [
                                                            [
                                                              '#type' => 'rev',
                                                              '#uuid' => $uuid,
                                                              '#rev' => $revs[15],
                                                              '#rev_info' => [
                                                                'status' => 'available',
                                                                'default' => TRUE,
                                                                'open_rev' => TRUE,
                                                                'conflict' => FALSE,
                                                              ],
                                                              'children' => [],
                                                            ],
                                                          ],
                                                        ],
                                                      ],
                                                    ],
                                                  ],
                                                ],
                                              ],
                                            ],
                                          ],
                                        ],
                                      ],
                                    ],
                                  ],
                                ],
                              ],
                            ],
                          ],
                        ],
                      ],
                    ],
                  ],
                ],
              ],
            ],
          ],
        ],
        [
          '#type' => 'rev',
          '#uuid' => $uuid,
          '#rev' => $revs[16],
          '#rev_info' => [
            'status' => 'available',
            'default' => FALSE,
            'open_rev' => TRUE,
            'conflict' => TRUE,
          ],
          'children' => [],
        ],
      ],
    ],
  ];

  // Sort the expected tree according to the algorithm.
  RevisionTreeIndex::sortTree($expected_tree);
  $tree = $this->revTree
    ->getTree($uuid);
  $this
    ->assertEqual($tree, $expected_tree, 'Tree was correctly parsed.');
  $default_rev = $this->revTree
    ->getDefaultRevision($uuid);
  $this
    ->assertEqual($default_rev, $revs[15], 'Default revision is correct.');
  $expected_default_branch = [
    $revs[0] => 'available',
    $revs[1] => 'available',
    $revs[3] => 'available',
    $revs[4] => 'available',
    $revs[5] => 'available',
    $revs[6] => 'available',
    $revs[7] => 'available',
    $revs[8] => 'available',
    $revs[9] => 'available',
    $revs[10] => 'available',
    $revs[11] => 'available',
    $revs[12] => 'available',
    $revs[13] => 'available',
    $revs[14] => 'available',
    $revs[15] => 'available',
  ];
  $default_branch = $this->revTree
    ->getDefaultBranch($uuid);
  $this
    ->assertEqual($default_branch, $expected_default_branch, 'Default branch is correct.');
  $count = $this->revTree
    ->countRevs($uuid);
  $this
    ->assertEqual($count, 15, 'Number of revisions is correct.');
  $expected_open_revision = [
    $revs[2] => 'available',
    $revs[15] => 'available',
    $revs[16] => 'available',
  ];
  $open_revisions = $this->revTree
    ->getOpenRevisions($uuid);
  $this
    ->assertEqual($open_revisions, $expected_open_revision, 'Open revisions are correct.');
  $expected_conflicts = [
    $revs[2] => 'available',
    $revs[16] => 'available',
  ];
  $conflicts = $this->revTree
    ->getConflicts($uuid);
  $this
    ->assertEqual($conflicts, $expected_conflicts, 'Conflicts are correct');
}