View source
<?php
namespace Drupal\Tests\apigee_edge\FunctionalJavascript;
use Apigee\Edge\Api\Management\Controller\DeveloperAppController;
use Apigee\Edge\Api\Management\Entity\App;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge\Entity\DeveloperApp;
use Drupal\Core\Url;
class CacheTest extends ApigeeEdgeFunctionalJavascriptTestBase {
protected $account;
protected $developer;
protected $developerApp;
protected $cacheBackend;
protected function setUp() {
parent::setUp();
$this->account = $this
->createAccount([
'create developer_app',
'view own developer_app',
'update own developer_app',
'delete own developer_app',
]);
$this->developer = Developer::load($this->account
->getEmail());
$this->developerApp = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => App::STATUS_APPROVED,
'developerId' => $this->developer
->uuid(),
]);
$this->developerApp
->save();
$this
->config('apigee_edge.common_app_settings')
->set('user_select', FALSE)
->save();
$this
->drupalLogin($this->account);
$this->cacheBackend = $this->container
->get('cache.apigee_edge_entity');
}
protected function tearDown() {
if ($this->developer !== NULL) {
try {
$this->developer
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
parent::tearDown();
}
public function testCache() {
$this
->warmCaches();
$this
->credentialsTest();
$this
->warmCaches();
$this
->editAppIsAlwaysUncachedTest();
$this
->warmCaches();
$this
->userUpdatedTest();
$this
->warmCaches();
$this
->userDeletedTest();
$this
->developerDeletedTest();
}
protected function credentialsTest() {
$storage = $this->container
->get('entity_type.manager')
->getStorage('developer_app');
$loadedApp = $storage
->load($this->developerApp
->id());
$this
->assertNotEmpty($loadedApp, 'Developer App loaded');
$cached_apps = $storage
->getFromCache([
$loadedApp
->id(),
]);
$cached_app = reset($cached_apps);
$this
->assertEmpty($cached_app
->decorated()
->getCredentials(), 'The credentials property is empty.');
$credentials = $loadedApp
->getCredentials();
$this
->assertNotEmpty($credentials, 'The credentials property is not empty.');
$this
->drupalGet(Url::fromRoute('entity.developer_app.canonical_by_developer', [
'user' => $this->account
->id(),
'app' => $this->developerApp
->getName(),
]));
$this
->assertSession()
->pageTextNotContains($credentials[0]
->getConsumerKey());
$this
->assertSession()
->pageTextNotContains($credentials[0]
->getConsumerSecret());
$this
->clickLink('Show key', 0);
$this
->assertSession()
->assertWaitOnAjaxRequest(30000);
$this
->clickLink('Show key', 1);
$this
->assertSession()
->assertWaitOnAjaxRequest(30000);
$this
->assertSession()
->pageTextContainsOnce($credentials[0]
->getConsumerKey());
$this
->assertSession()
->pageTextContainsOnce($credentials[0]
->getConsumerSecret());
$this
->clickLink('Hide key', 0);
$this
->assertSession()
->assertWaitOnAjaxRequest(30000);
$this
->clickLink('Hide key', 1);
$this
->assertSession()
->assertWaitOnAjaxRequest(30000);
$this
->assertSession()
->pageTextNotContains($credentials[0]
->getConsumerKey());
$this
->assertSession()
->pageTextNotContains($credentials[0]
->getConsumerSecret());
$this
->clickLink('Show key', 0);
$this
->assertSession()
->assertWaitOnAjaxRequest(30000);
$this
->clickLink('Show key', 1);
$this
->assertSession()
->assertWaitOnAjaxRequest(30000);
$this
->assertSession()
->pageTextContainsOnce($credentials[0]
->getConsumerKey());
$this
->assertSession()
->pageTextContainsOnce($credentials[0]
->getConsumerSecret());
}
protected function editAppIsAlwaysUncachedTest() {
$connector = $this->container
->get('apigee_edge.sdk_connector');
$controller = new DeveloperAppController($connector
->getOrganization(), $this->developer
->getDeveloperId(), $connector
->getClient());
$name = strtolower($this
->randomMachineName(16));
$developer_app = $controller
->load($this->developerApp
->getName());
$developer_app
->setDisplayName($name);
$controller
->update($developer_app);
$this
->drupalGet(Url::fromRoute('entity.developer_app.edit_form_for_developer', [
'user' => $this->account
->id(),
'app' => $developer_app
->getName(),
]));
$this
->assertSession()
->fieldValueEquals('displayName[0][value]', $name);
$this
->submitForm([], 'Save');
$this->developerApp
->setDisplayName($developer_app
->getDisplayName());
}
protected function userUpdatedTest() {
$this
->assertCacheInvalidation([
"values:developer:{$this->developer->id()}",
"values:developer_app:{$this->developerApp->id()}",
"app_names:developer_app:{$this->developer->uuid()}:{$this->developerApp->getName()}",
], function () {
$this
->drupalPostForm(Url::fromRoute('entity.user.edit_form', [
'user' => $this->account
->id(),
]), [
'first_name[0][value]' => $this
->randomMachineName(),
'last_name[0][value]' => $this
->randomMachineName(),
], 'Save');
});
}
protected function userDeletedTest() {
$this
->assertCacheInvalidation([
"values:developer:{$this->developer->id()}",
"values:developer_app:{$this->developerApp->id()}",
"app_names:developer_app:{$this->developer->uuid()}:{$this->developerApp->getName()}",
], function () {
$this
->drupalLogout();
$this->account
->delete();
$this->account = NULL;
});
}
public function developerDeletedTest() {
$data = [
'firstName' => $this
->randomString(),
'lastName' => $this
->randomString(),
'userName' => $this
->randomMachineName(),
];
$data['email'] = $this
->randomMachineName() . ".{$data['userName']}@example.com";
$developer = Developer::create($data);
$developer
->save();
$developer = Developer::load($developer
->id());
$developerApp = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => DeveloperApp::STATUS_APPROVED,
'developerId' => $developer
->uuid(),
]);
try {
$developerApp
->save();
} catch (\Exception $e) {
$developer
->delete();
throw $e;
}
$developerApp = DeveloperApp::load($developerApp
->id());
$this
->assertCacheInvalidation([
"values:developer:{$developer->id()}",
"values:developer:{$developer->uuid()}",
"values:developer_app:{$developerApp->id()}",
"values:developer_app:{$developerApp->uuid()}",
"app_names:developer_app:{$developer->uuid()}:{$developerApp->getName()}",
], function () use ($developer) {
try {
$developer
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
});
}
protected function warmCaches() {
$this
->drupalGet(Url::fromRoute('entity.developer_app.collection_by_developer', [
'user' => $this->account
->id(),
]));
$this
->clickLink($this->developerApp
->label());
}
protected function assertCacheInvalidation(array $keys, callable $action, bool $exists_before = TRUE, bool $exists_after = FALSE) {
$this
->assertKeys($keys, $exists_before);
$action();
$this
->assertKeys($keys, $exists_after);
}
protected function assertKeys(array $keys, bool $should_exist) {
foreach ($keys as $key) {
$value = $this->cacheBackend
->get($key);
if ($should_exist) {
$this
->assertNotFalse($value, "Cache key has not found when it should: {$key}");
}
else {
$this
->assertFalse($value, "Cache key found when it should not: {$key}");
}
}
}
}