diff --git a/modules/apigee_edge_teams/tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php b/modules/apigee_edge_teams/tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php new file mode 100644 index 00000000..149b38b5 --- /dev/null +++ b/modules/apigee_edge_teams/tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php @@ -0,0 +1,110 @@ +installConfig(['apigee_edge']); + $this->installConfig(['apigee_edge_teams']); + $this->installEntitySchema('user'); + $this->installEntitySchema('team_member_role'); + $this->installSchema('system', ['sequences']); + $this->installSchema('user', ['users_data']); + + $this->apigeeTestHelperSetup(); + $this->addApigeexOrganizationMatchedResponse(); + $this->entity = $this->createApigeexTeam(); + } + + /** + * Tests the cache max-age for the view builder. + */ + public function testViewCacheExpiration() { + /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ + $entity_type_manager = $this->container->get('entity_type.manager'); + $build = $entity_type_manager->getViewBuilder(static::ENTITY_TYPE)->view($this->entity); + + static::assertEquals(900, $build['#cache']['max-age']); + + // Update the cache setting. + $this->config('apigee_edge_teams.team_settings') + ->set('cache_expiration', 0) + ->save(); + + $build = $entity_type_manager->getViewBuilder(static::ENTITY_TYPE)->view($this->entity); + static::assertEquals(0, $build['#cache']['max-age']); + } + +} diff --git a/tests/modules/apigee_mock_api_client/tests/response-templates/appgroup.json.twig b/tests/modules/apigee_mock_api_client/tests/response-templates/appgroup.json.twig new file mode 100644 index 00000000..d4f4719d --- /dev/null +++ b/tests/modules/apigee_mock_api_client/tests/response-templates/appgroup.json.twig @@ -0,0 +1,25 @@ +{# +/** + * @file + * AppGroup + * + * Usage: + * @code {% include 'team.json.twig' %} @endcode + * + * Variables: + * - appgroup: The team (appgroup). + * - org_name: The org name. + */ +#} +{ + "apps": [], + "name": "{{ appgroup.name|default('foo') }}", + "displayName": "{{ appgroup.displayName }}", + "organization": "{{ org_name }}", + "status": "active", + "attributes": [], + "createdAt": 1506959878351, + "createdBy" : "user@example.com", + "lastModifiedAt": 1506959878351, + "lastModifiedBy" : "user@example.com" +} diff --git a/tests/modules/apigee_mock_api_client/tests/src/Traits/ApigeeMockApiClientHelperTrait.php b/tests/modules/apigee_mock_api_client/tests/src/Traits/ApigeeMockApiClientHelperTrait.php index 4f480abd..f6177fe6 100644 --- a/tests/modules/apigee_mock_api_client/tests/src/Traits/ApigeeMockApiClientHelperTrait.php +++ b/tests/modules/apigee_mock_api_client/tests/src/Traits/ApigeeMockApiClientHelperTrait.php @@ -19,6 +19,7 @@ namespace Drupal\Tests\apigee_mock_api_client\Traits; +use Apigee\Edge\Api\ApigeeX\Entity\AppGroup; use Apigee\Edge\Api\Management\Entity\App; use Apigee\Edge\Api\Management\Entity\Company; use Apigee\Edge\Api\Management\Entity\Organization; @@ -152,6 +153,31 @@ protected function addApigeexOrganizationMatchedResponse($organizationName = '', ); } + /** + * Helper function to queue up an Apigee X org response since every test will need it. + * + * @param string $runtimetype + * Whether or not the org is cloud, hybrid or non-hybrid. + * @param bool $monetized + * Whether or not the org is monetized. + * + * @throws \Exception + */ + protected function warmApigeexOrganizationCache($runtimetype = 'CLOUD', $monetized = TRUE) { + if (!$this->sdkConnector->getOrganization()) { + $this->addApigeexOrganizationMatchedResponse(); + } + $this->stack + ->queueMockResponse([ + 'get_apigeex_organization' => [ + 'runtimetype' => $runtimetype, + 'monetization_enabled' => $monetized ? 'true' : 'false', + 'timezone' => $this->org_default_timezone, + ], + ]); + $this->sdkConnector->getOrganization(); + } + /** * Add matched developer response. * @@ -240,6 +266,23 @@ protected function queueCompanyResponse(Company $company, $response_code = NULL) $this->stack->queueMockResponse(['company' => $context]); } + /** + * Queues up a mock appgroup response. + * + * @param \Apigee\Edge\Api\ApigeeX\Entity\AppGroup $appgroup + * The appgroup to get properties from. + * @param string|null $response_code + * Add a response code to override the default. + */ + protected function queueAppGroupResponse(AppGroup $appgroup, $response_code = NULL) { + $context = empty($response_code) ? [] : ['status_code' => $response_code]; + + $context['appgroup'] = $appgroup; + $context['org_name'] = $this->sdkConnector->getOrganization(); + + $this->stack->queueMockResponse(['appgroup' => $context]); + } + /** * Queues up a mock companies response. * @@ -319,6 +362,28 @@ protected function createTeam(): TeamInterface { return $team; } + /** + * Helper to create a Apigee X Team entity. + * + * @return \Drupal\apigee_edge_teams\Entity\TeamInterface + * A Team entity. + * + * @throws \Drupal\Core\Entity\EntityStorageException + */ + protected function createApigeexTeam(): TeamInterface { + /** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */ + $team = Team::create([ + 'name' => $this->randomMachineName(), + 'displayName' => $this->randomGenerator->name(), + ]); + + $this->queueAppGroupResponse($team->decorated()); + $this->stack->queueMockResponse('no_content'); + $team->save(); + + return $team; + } + /** * Adds a user to a team. *