View source
<?php
namespace Drupal\Tests\apigee_api_catalog\Kernel;
use Drupal\KernelTests\KernelTestBase;
class ApidocEntityTest extends KernelTestBase {
protected $entityTypeManager;
protected $nodeStorage;
protected static $modules = [
'user',
'node',
'field',
'system',
'options',
'text',
'file',
'link',
'file_link',
'path_alias',
'apigee_api_catalog',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('user', [
'users_data',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('node_type');
$this
->installEntitySchema('path_alias');
$this
->installConfig(static::$modules);
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->nodeStorage = $this->entityTypeManager
->getStorage('node');
}
public function testEntity() {
$entity = $this->nodeStorage
->create([
'type' => 'apidoc',
'title' => 'API 1',
'body' => [
'value' => 'Test API 1',
'format' => 'basic_html',
],
'field_apidoc_spec' => NULL,
]);
$this
->assertNotNull($entity);
$this
->assertEquals(SAVED_NEW, $entity
->save());
$this
->assertEquals(SAVED_UPDATED, $entity
->set('title', 'API 1a')
->save());
$entity_id = $entity
->id();
$this
->assertNotEmpty($entity_id);
$entity
->toUrl()
->toString();
$alias = \Drupal::service('path_alias.manager')
->getAliasByPath('/node/' . $entity
->id(), $entity
->language()
->getId());
$this
->assertEqual($alias, '/api/' . $entity
->id());
$entity
->delete();
$this
->assertNull($this->nodeStorage
->load($entity_id));
}
public function testRevisions() {
$description_v1 = 'Test API';
$entity = $this->nodeStorage
->create([
'type' => 'apidoc',
'title' => 'API 1',
'body' => [
'value' => $description_v1,
'format' => 'basic_html',
],
'field_apidoc_spec' => NULL,
]);
$entity
->setNewRevision();
$entity
->setRevisionLogMessage('v1');
$entity
->save();
$v1_id = $entity
->getRevisionId();
$this
->assertNotNull($v1_id);
$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);
$entity->body = [
'value' => 'Test API v3',
'format' => 'basic_html',
];
$entity
->save();
$this
->assertEquals($v2_id, $entity
->getRevisionId());
$this
->assertEquals($new_log, $entity
->getRevisionLogMessage());
$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();
$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);
}
}