Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appgroup testcase for Team view builder #955

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* Copyright 2023 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

namespace Drupal\Tests\apigee_edge_teams\Kernel\ApigeeX\Entity;

use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\apigee_edge\Kernel\ApigeeEdgeKernelTestTrait;
use Drupal\Tests\apigee_mock_api_client\Traits\ApigeeMockApiClientHelperTrait;

/**
* Tests the Team view builder.
*
* @group apigee_edge
* @group apigee_edge_kernel
* @group apigee_edge_teams
* @group apigee_edge_teams_kernel
*/
class TeamViewBuilderTest extends KernelTestBase {

use ApigeeMockApiClientHelperTrait, ApigeeEdgeKernelTestTrait;

/**
* Indicates this test class is mock API client ready.
*
* @var bool
*/
protected static $mock_api_client_ready = TRUE;

/**
* The entity type to test.
*/
const ENTITY_TYPE = 'team';

/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'apigee_edge',
'apigee_edge_teams',
'apigee_mock_api_client',
'key',
'user',
'options'
];

/**
* The team entity.
*
* @var \Drupal\apigee_edge_teams\Entity\TeamInterface
*/
protected $entity;

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function setUp(): void {
parent::setUp();

$this->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']);
}

}
Original file line number Diff line number Diff line change
@@ -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" : "[email protected]",
"lastModifiedAt": 1506959878351,
"lastModifiedBy" : "[email protected]"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down