View source
<?php
namespace Drupal\Tests\apigee_edge\Kernel\Entity\ListBuilder;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\apigee_edge\Kernel\ApigeeEdgeKernelTestTrait;
use Drupal\Tests\apigee_mock_api_client\Traits\ApigeeMockApiClientHelperTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;
class EntityListBuilderTest extends KernelTestBase {
use ApigeeMockApiClientHelperTrait, ApigeeEdgeKernelTestTrait, UserCreationTrait;
protected static $mock_api_client_ready = TRUE;
const ENTITY_TYPE = 'developer_app';
protected static $modules = [
'system',
'apigee_edge',
'apigee_mock_api_client',
'key',
'user',
'options',
];
protected $account;
protected $app;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installConfig([
'apigee_edge',
]);
$this
->apigeeTestHelperSetup();
$this
->addOrganizationMatchedResponse();
$this->account = User::create([
'mail' => $this
->randomMachineName() . '@example.com',
'name' => $this
->randomMachineName(),
'first_name' => $this
->getRandomGenerator()
->word(16),
'last_name' => $this
->getRandomGenerator()
->word(16),
]);
$this->account
->save();
}
protected function tearDown() {
$this->stack
->reset();
try {
if ($this->account) {
$this
->queueDeveloperResponse($this->account);
$developer = \Drupal::entityTypeManager()
->getStorage('developer')
->create([
'email' => $this->account
->getEmail(),
]);
$developer
->delete();
}
if ($this->app) {
$this->app
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
parent::tearDown();
}
public function testDisplaySettings() {
$entity_type_manager = $this->container
->get('entity_type.manager');
$this->app = $this
->createDeveloperApp();
$this->stack
->queueMockResponse([
'get_developer_apps' => [
'apps' => [
$this->app,
],
],
]);
$build = $entity_type_manager
->getListBuilder(static::ENTITY_TYPE)
->render();
static::assertTrue(isset($build['table']));
EntityViewMode::create([
'id' => static::ENTITY_TYPE . '.foo',
'targetEntityType' => static::ENTITY_TYPE,
'label' => 'Foo',
'status' => TRUE,
])
->save();
$config = $this
->config('apigee_edge.display_settings.' . static::ENTITY_TYPE);
$config
->set('display_type', 'view_mode')
->set('view_mode', 'foo')
->save();
$build = $entity_type_manager
->getListBuilder(static::ENTITY_TYPE)
->render();
static::assertSame('apigee_entity_list', $build['#type']);
static::assertSame('foo', $build['#view_mode']);
}
public function testCacheSettings() {
$this->app = $this
->createDeveloperApp();
$this
->setCurrentUser($this->account);
$url = Url::fromRoute('entity.developer_app.collection_by_developer', [
'user' => $this->account
->id(),
]);
$request = Request::create($url
->toString(), 'GET');
$this->stack
->queueMockResponse([
'get_developer_apps_names' => [
'apps' => [
$this->app,
],
],
]);
$this
->queueDeveloperResponse($this->account);
$response = $this->container
->get('http_kernel')
->handle($request);
$this
->assertEqual($response
->getCacheableMetadata()
->getCacheMaxAge(), 900);
$config = $this
->config('apigee_edge.' . static::ENTITY_TYPE . '_settings');
$config
->set('cache_expiration', 100)
->save();
$this->stack
->queueMockResponse([
'get_developer_apps_names' => [
'apps' => [
$this->app,
],
],
]);
$this
->queueDeveloperResponse($this->account);
$response = $this->container
->get('http_kernel')
->handle($request);
$this
->assertEqual($response
->getCacheableMetadata()
->getCacheMaxAge(), 100);
}
}