View source
<?php
namespace Drupal\Tests\apigee_api_catalog\Kernel;
use Drupal\apigee_api_catalog\Entity\ApiDoc;
use Drupal\KernelTests\KernelTestBase;
class ApidocEntityTest extends KernelTestBase {
protected $entityTypeManager;
protected static $modules = [
'user',
'system',
'apigee_edge',
'key',
'apigee_api_catalog',
'options',
'text',
'file',
'file_link',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('apidoc');
$this->entityTypeManager = $this->container
->get('entity_type.manager');
}
public function testEntity() {
$entity = ApiDoc::create([
'name' => 'API 1',
'description' => 'Test API 1',
'spec' => NULL,
'api_product' => NULL,
]);
$this
->assertNotNull($entity);
$this
->assertEquals(SAVED_NEW, $entity
->save());
$this
->assertEquals(SAVED_UPDATED, $entity
->set('name', 'API 1a')
->save());
$entity_id = $entity
->id();
$this
->assertNotEmpty($entity_id);
$entity
->delete();
$this
->assertNull(ApiDoc::load($entity_id));
}
public function testRevisions() {
$description_v1 = 'Test API';
$entity = ApiDoc::create([
'name' => 'API 1',
'description' => $description_v1,
'spec' => NULL,
'api_product' => NULL,
]);
$entity
->setNewRevision();
$entity
->setRevisionLogMessage('v1');
$entity
->save();
$v1_id = $entity
->getRevisionId();
$this
->assertNotNull($v1_id);
$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);
$entity
->setDescription('Test API v3');
$entity
->save();
$this
->assertEquals($v2_id, $entity
->getRevisionId());
$this
->assertEquals($new_log, $entity
->getRevisionLogMessage());
$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();
$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());
}
public function testEntityBaseFieldNameLength() {
$entity = ApiDoc::create([
'name' => $this
->randomString(100),
'description' => 'Test API 1',
'spec' => NULL,
'api_product' => NULL,
]);
$this
->assertNotNull($entity);
$this
->assertEquals(SAVED_NEW, $entity
->save());
}
}