You are here

public function ApidocEntityTest::testRevisions in Apigee API Catalog 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/ApidocEntityTest.php \Drupal\Tests\apigee_api_catalog\Kernel\ApidocEntityTest::testRevisions()

Test revisioning functionality on an apidocs entity.

File

tests/src/Kernel/ApidocEntityTest.php, line 86

Class

ApidocEntityTest
Test basic CRUD operations for our ApiDoc entity type.

Namespace

Drupal\Tests\apigee_api_catalog\Kernel

Code

public function testRevisions() {
  $description_v1 = 'Test API';
  $entity = ApiDoc::create([
    'name' => 'API 1',
    'description' => $description_v1,
    'spec' => NULL,
    'api_product' => NULL,
  ]);

  // Test saving a revision.
  $entity
    ->setNewRevision();
  $entity
    ->setRevisionLogMessage('v1');
  $entity
    ->save();
  $v1_id = $entity
    ->getRevisionId();
  $this
    ->assertNotNull($v1_id);

  // Test saving a new revision.
  $new_log = 'v2';
  $entity
    ->setDescription('Test API v2');
  $entity
    ->setNewRevision();
  $entity
    ->setRevisionLogMessage($new_log);
  $entity
    ->save();
  $v2_id = $entity
    ->getRevisionId();
  $this
    ->assertLessThan($v2_id, $v1_id);

  // Test saving without a new revision.
  $entity
    ->setDescription('Test API v3');
  $entity
    ->save();
  $this
    ->assertEquals($v2_id, $entity
    ->getRevisionId());

  // Test that the revision log message wasn't overriden.
  $this
    ->assertEquals($new_log, $entity
    ->getRevisionLogMessage());

  // Revert to the first revision.
  $entity_v1 = $this->entityTypeManager
    ->getStorage('apidoc')
    ->loadRevision($v1_id);
  $entity_v1
    ->setNewRevision();
  $entity_v1
    ->isDefaultRevision(TRUE);
  $entity_v1
    ->setRevisionLogMessage('Copy of revision ' . $v1_id);
  $entity_v1
    ->save();

  // Load and check reverted values.
  $this->entityTypeManager
    ->getStorage('apidoc')
    ->resetCache();
  $reverted = ApiDoc::load($entity
    ->id());
  $this
    ->assertLessThan($reverted
    ->getRevisionId(), $v1_id);
  $this
    ->assertTrue($reverted
    ->isDefaultRevision());
  $this
    ->assertEquals($description_v1, $reverted
    ->getDescription());
}