From 843e68536a3f0cf7610b3912328cc08c34ea45d0 Mon Sep 17 00:00:00 2001 From: Shishir Suvarna Date: Tue, 10 Oct 2023 12:29:20 +0530 Subject: [PATCH 1/4] Testcase for AppGroup team view builder --- .../ApigeeX/Entity/TeamViewBuilderTest.php | 110 ++++++++++++++++++ .../response-templates/appgroup.json.twig | 25 ++++ .../Traits/ApigeeMockApiClientHelperTrait.php | 65 +++++++++++ 3 files changed, 200 insertions(+) create mode 100644 modules/apigee_edge_teams/tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php create mode 100644 tests/modules/apigee_mock_api_client/tests/response-templates/appgroup.json.twig 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..6b0530e0 --- /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->createXTeam(); + } + + /** + * 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..0bf88242 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 @@ -21,6 +21,7 @@ use Apigee\Edge\Api\Management\Entity\App; use Apigee\Edge\Api\Management\Entity\Company; +use Apigee\Edge\Api\ApigeeX\Entity\AppGroup; use Apigee\Edge\Api\Management\Entity\Organization; use Apigee\Edge\Structure\AddonsConfig; use Apigee\Edge\Structure\MonetizationConfig; @@ -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. * From 2ec2de7933e0efbbdc202de67083558c8c5c415c Mon Sep 17 00:00:00 2001 From: Shishir Suvarna Date: Tue, 10 Oct 2023 13:32:36 +0530 Subject: [PATCH 2/4] fix phpcs error --- .../tests/src/Traits/ApigeeMockApiClientHelperTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0bf88242..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,9 +19,9 @@ 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\ApigeeX\Entity\AppGroup; use Apigee\Edge\Api\Management\Entity\Organization; use Apigee\Edge\Structure\AddonsConfig; use Apigee\Edge\Structure\MonetizationConfig; From e9cd8606684853c19b5d5ccb2e1c6c428a4a5e02 Mon Sep 17 00:00:00 2001 From: Shishir Suvarna Date: Tue, 10 Oct 2023 17:51:23 +0530 Subject: [PATCH 3/4] create team method name refactored --- .../tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 6b0530e0..9bbed6fb 100644 --- a/modules/apigee_edge_teams/tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php +++ b/modules/apigee_edge_teams/tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php @@ -85,7 +85,7 @@ protected function setUp(): void { $this->apigeeTestHelperSetup(); $this->addApigeexOrganizationMatchedResponse(); - $this->entity = $this->createXTeam(); + $this->entity = $this->createApigeexTeam(); } /** From a98588a5a9a11a59f3b0eacc39e0488dfe4672ea Mon Sep 17 00:00:00 2001 From: Shishir Suvarna Date: Thu, 12 Oct 2023 11:57:31 +0530 Subject: [PATCH 4/4] Namespace refactored --- .../tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9bbed6fb..149b38b5 100644 --- a/modules/apigee_edge_teams/tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php +++ b/modules/apigee_edge_teams/tests/src/Kernel/ApigeeX/Entity/TeamViewBuilderTest.php @@ -18,7 +18,7 @@ * MA 02110-1301, USA. */ -namespace Drupal\Tests\apigee_edge_teams\ApigeeX\Kernel\Entity; +namespace Drupal\Tests\apigee_edge_teams\Kernel\ApigeeX\Entity; use Drupal\KernelTests\KernelTestBase; use Drupal\Tests\apigee_edge\Kernel\ApigeeEdgeKernelTestTrait;