You are here

public function ApidocEntityTest::testRevisions in Apigee API Catalog 8.2

Same name and namespace in other branches
  1. 8 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 114

Class

ApidocEntityTest
Test basic CRUD operations for ApiDoc.

Namespace

Drupal\Tests\apigee_api_catalog\Kernel

Code

public function testRevisions() {

  /* @var \Drupal\node\NodeInterface $entity */
  $description_v1 = 'Test API';
  $entity = $this->nodeStorage
    ->create([
    'type' => 'apidoc',
    'title' => 'API 1',
    'body' => [
      'value' => $description_v1,
      'format' => 'basic_html',
    ],
    'field_apidoc_spec' => 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->body = [
    'value' => 'Test API v2',
    'format' => 'basic_html',
  ];
  $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->body = [
    'value' => 'Test API v3',
    'format' => 'basic_html',
  ];
  $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->nodeStorage
    ->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->nodeStorage
    ->resetCache();
  $reverted = $this->nodeStorage
    ->load($entity
    ->id());
  $this
    ->assertLessThan($reverted
    ->getRevisionId(), $v1_id);
  $this
    ->assertTrue($reverted
    ->isDefaultRevision());
  $this
    ->assertEquals($description_v1, $reverted->body->value);
}