From 17c30dab39460220fa9adfb8b8cc9ec9251fd96d Mon Sep 17 00:00:00 2001 From: Arlina Espinoza Date: Fri, 5 Jun 2020 00:56:38 -0700 Subject: [PATCH 1/7] [#408] Fix team lists cache errors. --- .../src/Entity/ListBuilder/TeamListBuilder.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php index 432cf1f8..70dc71e5 100644 --- a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php +++ b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php @@ -110,4 +110,18 @@ public function buildRow(EntityInterface $entity) { return $row + parent::buildRow($entity); } + /** + * {@inheritdoc} + */ + public function render() { + $build = parent::render(); + + // Team lists vary for each user. + $build['#cache']['contexts'][] = 'user'; + $build['#cache']['tags'][] = 'user:' . \Drupal::currentUser()->id(); + $build['#cache']['max-age'] = 0; + + return $build; + } + } From 872759ec6a4b566681690449c4d15442ac7c7f6e Mon Sep 17 00:00:00 2001 From: Arlina Espinoza Date: Mon, 8 Jun 2020 02:28:13 -0700 Subject: [PATCH 2/7] [#408] Fix team lists cache errors - updated cache tags. --- .../Entity/ListBuilder/TeamListBuilder.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php index 70dc71e5..9ca777e1 100644 --- a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php +++ b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php @@ -115,11 +115,26 @@ public function buildRow(EntityInterface $entity) { */ public function render() { $build = parent::render(); + $account = $this->entityTypeManager->getStorage('user')->load(\Drupal::currentUser()->id()); // Team lists vary for each user. $build['#cache']['contexts'][] = 'user'; - $build['#cache']['tags'][] = 'user:' . \Drupal::currentUser()->id(); - $build['#cache']['max-age'] = 0; + + // This team list should be invalidated if the user's permissions change. + $build['#cache']['tags'][] = 'user:' . $account->id(); + + // This team list should be invalidated if the user roles permissions change. + foreach ($account->getRoles() as $rid) { + $build['#cache']['tags'][] = "config:user.role.$rid"; + } + + // This team list should be invalidated if the team roles permissions change. + $build['#cache']['tags'][] = 'team_role_list'; + + // Use cache expiration defined in configuration. + $build['#cache']['max-age'] = $this->configFactory + ->get('apigee_edge_teams.team_settings') + ->get('cache_expiration'); return $build; } From 18b7fc3b31b6069dd986f13c89740a8d62136974 Mon Sep 17 00:00:00 2001 From: Arlina Espinoza Date: Mon, 8 Jun 2020 23:24:55 -0700 Subject: [PATCH 3/7] [#408] Fix team lists cache errors - updates and test. --- .../Entity/ListBuilder/TeamListBuilder.php | 22 +- .../tests/src/Kernel/TeamListBuilderTest.php | 210 ++++++++++++++++++ .../response-templates/companies.json.twig | 19 ++ .../Traits/ApigeeMockApiClientHelperTrait.php | 33 ++- 4 files changed, 268 insertions(+), 16 deletions(-) create mode 100644 modules/apigee_edge_teams/tests/src/Kernel/TeamListBuilderTest.php create mode 100644 tests/modules/apigee_mock_api_client/tests/response-templates/companies.json.twig diff --git a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php index 9ca777e1..595fbdee 100644 --- a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php +++ b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php @@ -115,21 +115,17 @@ public function buildRow(EntityInterface $entity) { */ public function render() { $build = parent::render(); - $account = $this->entityTypeManager->getStorage('user')->load(\Drupal::currentUser()->id()); - // Team lists vary for each user. - $build['#cache']['contexts'][] = 'user'; - - // This team list should be invalidated if the user's permissions change. - $build['#cache']['tags'][] = 'user:' . $account->id(); + // Team lists vary for each user and their permissions. + // Note: Even though cache contexts will be optimized to only include the + // 'user' cache context, the element should be invalidated correctly when + // permissions change because the 'user.permissions' cache context defined + // cache tags for permission changes, which should have bubbled up for the + // element when it was optimized away. + // @see \Drupal\KernelTests\Core\Cache\CacheContextOptimizationTest - // This team list should be invalidated if the user roles permissions change. - foreach ($account->getRoles() as $rid) { - $build['#cache']['tags'][] = "config:user.role.$rid"; - } - - // This team list should be invalidated if the team roles permissions change. - $build['#cache']['tags'][] = 'team_role_list'; + $build['#cache']['contexts'][] = 'user'; + $build['#cache']['contexts'][] = 'user.permissions'; // Use cache expiration defined in configuration. $build['#cache']['max-age'] = $this->configFactory diff --git a/modules/apigee_edge_teams/tests/src/Kernel/TeamListBuilderTest.php b/modules/apigee_edge_teams/tests/src/Kernel/TeamListBuilderTest.php new file mode 100644 index 00000000..e7ee7dfc --- /dev/null +++ b/modules/apigee_edge_teams/tests/src/Kernel/TeamListBuilderTest.php @@ -0,0 +1,210 @@ +installEntitySchema('user'); + $this->installEntitySchema('team_role'); + $this->installEntitySchema('team_member_role'); + $this->installSchema('system', ['sequences']); + $this->installConfig(['user', 'apigee_edge', 'apigee_edge_teams']); + + $this->apigeeTestHelperSetup(); + + $this->teamStorage = $this->entityTypeManager->getStorage('team'); + + $config_factory = \Drupal::configFactory(); + $config = $config_factory->getEditable('apigee_edge_teams.team_settings'); + $config->set('cache_expiration', 300); + $config->save(TRUE); + + // Create teams. + $this->teamA = $this->createTeam(); + $this->teamB = $this->createTeam(); + + // Create accounts: user 1, and for members of two teams. + $this->account = $this->createAccount(); + $this->aMemberAccount = $this->createAccount(); + $this->bMemberAccount = $this->createAccount(); + + // Add accounts to teams. + $this->addUserToTeam($this->teamA, $this->aMemberAccount); + $this->addUserToTeam($this->teamB, $this->bMemberAccount); + + $this->addOrganizationMatchedResponse(); + } + + /** + * {@inheritdoc} + */ + protected function tearDown() { + try { + $this->teamStorage->delete([$this->teamA, $this->teamB]); + $this->account->delete(); + $this->aMemberAccount->delete(); + $this->bMemberAccount->delete(); + } + catch (\Error $error) { + // Do nothing. + } + } + + /** + * Tests team list cache. + */ + public function testTeamListCache() { + $accountSwitcher = \Drupal::service('account_switcher'); + + $accountSwitcher->switchTo($this->aMemberAccount); + + $this->setRawContent($this->getRenderedList()); + $this->assertText($this->teamA->label()); + $this->assertNoText($this->teamB->label()); + + $accountSwitcher->switchTo($this->bMemberAccount); + + $this->setRawContent($this->getRenderedList()); + $this->assertNoText($this->teamA->label()); + $this->assertText($this->teamB->label()); + } + + /** + * Helper function to get the HTML output of the Team List. + * + * @return \Drupal\Component\Render\MarkupInterface + * The rendered HTML. + */ + protected function getRenderedList() { + $renderer = \Drupal::service('renderer'); + $listBuilder = $this->entityTypeManager->getListBuilder('team'); + + $this->stack->queueMockResponse(['companies' => [ + 'companies' => [$this->teamA->decorated(), $this->teamB->decorated()], + ]]); + $element = $listBuilder->render(); + $element['#cache']['keys'] = ['test']; + + return $renderer->renderRoot($element); + } + + /** + * Helper function to create a random user account. + * + * @return \Drupal\Core\Entity\EntityInterface + * The user account. + */ + protected function createAccount() { + $account = $this->entityTypeManager->getStorage('user')->create([ + 'mail' => $this->randomMachineName() . '@example.com', + 'name' => $this->randomMachineName(), + 'first_name' => $this->getRandomGenerator()->word(16), + 'last_name' => $this->getRandomGenerator()->word(16), + ]); + $this->queueDeveloperResponse($account, Response::HTTP_CREATED); + $account->save(); + + return $account; + } + +} diff --git a/tests/modules/apigee_mock_api_client/tests/response-templates/companies.json.twig b/tests/modules/apigee_mock_api_client/tests/response-templates/companies.json.twig new file mode 100644 index 00000000..bee0379a --- /dev/null +++ b/tests/modules/apigee_mock_api_client/tests/response-templates/companies.json.twig @@ -0,0 +1,19 @@ +{# +/** + * @file + * Companies + * + * Usage: + * @code {% include 'companies.json.twig' %} @endcode + * + * Variables: + * - companies: an array of company objects. + */ +#} +{ + "company" : [ + {% for company in companies %} + {% include 'company.json.twig' with {'company': company} %}{{ loop.last ? '' : ',' }} + {% endfor %} + ] +} 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 d1109724..2aab3c35 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 @@ -260,19 +260,46 @@ protected function createDeveloperApp(): DeveloperAppInterface { * * @throws \Drupal\Core\Entity\EntityStorageException */ - protected function createTeam(): TeamInterface { + protected function createTeam(): TeamInterface {static $i = 0; /** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */ $team = Team::create([ - 'name' => $this->randomMachineName(), + 'name' => $this->randomMachineName() . '_' . $i++, 'displayName' => $this->randomGenerator->name(), ]); $this->queueCompanyResponse($team->decorated()); - $this->queueDeveloperResponse($this->account); + $this->stack->queueMockResponse('no_content'); $team->save(); return $team; } + /** + * Adds a user to a team. + * + * Adding a team to a user will add the team as long as the developer entity + * is loaded from cache. + * + * @param \Drupal\apigee_edge_teams\Entity\TeamInterface $team + * The team. + * @param \Drupal\user\UserInterface $user + * A drupal user. + * + * @return \Drupal\apigee_edge\Entity\DeveloperInterface + * The developer entity. + */ + public function addUserToTeam(TeamInterface $team, UserInterface $user) { + $context['developer'] = $user; + $context['org_name'] = $this->sdkConnector->getOrganization(); + $context['companies'] = [$team->id()]; + + $this->stack->queueMockResponse(['get_developer' => $context]); + + $teams = \Drupal::service('apigee_edge_teams.team_membership_manager')->getTeams($user->getEmail()); + static::assertSame([$team->id()], $teams); + + return $this->entityTypeManager->getStorage('developer')->load($user->getEmail()); + } + /** * Helper to add Edge entity response to stack. * From 1a1f18747263ae02ebc6c35ffe558934ce1a2871 Mon Sep 17 00:00:00 2001 From: Arlina Espinoza Date: Thu, 11 Jun 2020 12:56:15 -0700 Subject: [PATCH 4/7] [#408] Fix team lists cache errors - updates and test. --- .../Entity/ListBuilder/TeamListBuilder.php | 9 +- .../TeamListBuilderTest.php | 150 ++++++++++-------- .../Traits/ApigeeMockApiClientHelperTrait.php | 34 +++- 3 files changed, 121 insertions(+), 72 deletions(-) rename modules/apigee_edge_teams/tests/src/{Kernel => Functional}/TeamListBuilderTest.php (50%) diff --git a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php index 595fbdee..a1a04e3a 100644 --- a/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php +++ b/modules/apigee_edge_teams/src/Entity/ListBuilder/TeamListBuilder.php @@ -23,6 +23,7 @@ use Drupal\apigee_edge\Element\StatusPropertyElement; use Drupal\apigee_edge\Entity\ListBuilder\EdgeEntityListBuilder; use Drupal\apigee_edge_teams\Entity\TeamInterface; +use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Url; @@ -115,6 +116,11 @@ public function buildRow(EntityInterface $entity) { */ public function render() { $build = parent::render(); + $account = $this->entityTypeManager->getStorage('user')->load(\Drupal::currentUser()->id()); + + $build = empty($build['table']) ? $build : $build['table']; + + $build['#cache']['keys'][] = 'team_list_per_user'; // Team lists vary for each user and their permissions. // Note: Even though cache contexts will be optimized to only include the @@ -123,10 +129,11 @@ public function render() { // cache tags for permission changes, which should have bubbled up for the // element when it was optimized away. // @see \Drupal\KernelTests\Core\Cache\CacheContextOptimizationTest - $build['#cache']['contexts'][] = 'user'; $build['#cache']['contexts'][] = 'user.permissions'; + $build['#cache']['tags'] = Cache::mergeTags($build['#cache']['tags'], $account->getCacheTags()); + // Use cache expiration defined in configuration. $build['#cache']['max-age'] = $this->configFactory ->get('apigee_edge_teams.team_settings') diff --git a/modules/apigee_edge_teams/tests/src/Kernel/TeamListBuilderTest.php b/modules/apigee_edge_teams/tests/src/Functional/TeamListBuilderTest.php similarity index 50% rename from modules/apigee_edge_teams/tests/src/Kernel/TeamListBuilderTest.php rename to modules/apigee_edge_teams/tests/src/Functional/TeamListBuilderTest.php index e7ee7dfc..8b61eb6e 100644 --- a/modules/apigee_edge_teams/tests/src/Kernel/TeamListBuilderTest.php +++ b/modules/apigee_edge_teams/tests/src/Functional/TeamListBuilderTest.php @@ -17,24 +17,19 @@ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -namespace Drupal\Tests\apigee_edge_teams\Kernel; +namespace Drupal\Tests\apigee_edge_teams\Functional; -use Drupal\KernelTests\KernelTestBase; -use Drupal\Tests\apigee_mock_api_client\Traits\ApigeeMockApiClientHelperTrait; -use Drupal\Tests\user\Traits\UserCreationTrait; +use Drupal\apigee_edge\Entity\Developer; +use Drupal\Core\Url; use Symfony\Component\HttpFoundation\Response; /** * Apigee Edge Teams list builder tests. * * @group apigee_edge - * @group apigee_edge_kernel * @group apigee_edge_teams - * @group apigee_edge_teams_kernel */ -class TeamListBuilderTest extends KernelTestBase { - - use ApigeeMockApiClientHelperTrait, UserCreationTrait; +class TeamListBuilderTest extends ApigeeEdgeTeamsFunctionalTestBase { /** * Indicates this test class is mock API client ready. @@ -84,6 +79,13 @@ class TeamListBuilderTest extends KernelTestBase { */ protected $bMemberAccount; + /** + * Drupal user who is an admin. + * + * @var \Drupal\user\UserInterface + */ + protected $cMemberAccount; + /** * Team A entity to test. * @@ -98,19 +100,16 @@ class TeamListBuilderTest extends KernelTestBase { */ protected $teamB; + + protected $customRole; + /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - $this->installEntitySchema('user'); - $this->installEntitySchema('team_role'); - $this->installEntitySchema('team_member_role'); - $this->installSchema('system', ['sequences']); - $this->installConfig(['user', 'apigee_edge', 'apigee_edge_teams']); - - $this->apigeeTestHelperSetup(); + $this->addOrganizationMatchedResponse(); $this->teamStorage = $this->entityTypeManager->getStorage('team'); @@ -119,20 +118,21 @@ protected function setUp() { $config->set('cache_expiration', 300); $config->save(TRUE); + // Create accounts: user 1, for members of two teams, and an extra one. + $this->account = $this->rootUser; + $this->aMemberAccount = $this->createNewAccount(); + $this->bMemberAccount = $this->createNewAccount(); + $this->cMemberAccount = $this->createNewAccount(); + + $this->customRole = $this->drupalCreateRole(['view any team']); + // Create teams. $this->teamA = $this->createTeam(); $this->teamB = $this->createTeam(); - // Create accounts: user 1, and for members of two teams. - $this->account = $this->createAccount(); - $this->aMemberAccount = $this->createAccount(); - $this->bMemberAccount = $this->createAccount(); - // Add accounts to teams. $this->addUserToTeam($this->teamA, $this->aMemberAccount); $this->addUserToTeam($this->teamB, $this->bMemberAccount); - - $this->addOrganizationMatchedResponse(); } /** @@ -144,48 +144,65 @@ protected function tearDown() { $this->account->delete(); $this->aMemberAccount->delete(); $this->bMemberAccount->delete(); + $this->cMemberAccount->delete(); } catch (\Error $error) { // Do nothing. } + catch (\Exception $exception) { + // Do nothing. + } } /** * Tests team list cache. */ public function testTeamListCache() { - $accountSwitcher = \Drupal::service('account_switcher'); - - $accountSwitcher->switchTo($this->aMemberAccount); - - $this->setRawContent($this->getRenderedList()); - $this->assertText($this->teamA->label()); - $this->assertNoText($this->teamB->label()); - - $accountSwitcher->switchTo($this->bMemberAccount); - - $this->setRawContent($this->getRenderedList()); - $this->assertNoText($this->teamA->label()); - $this->assertText($this->teamB->label()); - } - - /** - * Helper function to get the HTML output of the Team List. - * - * @return \Drupal\Component\Render\MarkupInterface - * The rendered HTML. - */ - protected function getRenderedList() { - $renderer = \Drupal::service('renderer'); - $listBuilder = $this->entityTypeManager->getListBuilder('team'); - - $this->stack->queueMockResponse(['companies' => [ - 'companies' => [$this->teamA->decorated(), $this->teamB->decorated()], - ]]); - $element = $listBuilder->render(); - $element['#cache']['keys'] = ['test']; - - return $renderer->renderRoot($element); + $companies = [ + $this->teamA->decorated(), + $this->teamB->decorated(), + ]; + + // aMemberAccount should only see teamA. + $this->drupalLogin($this->aMemberAccount); + $this->queueCompaniesResponse($companies); + $this->queueDeveloperResponse($this->aMemberAccount, 200, ['companies' => [$this->teamA->id()]]); + $this->drupalGet(Url::fromRoute('entity.team.collection')); + $assert = $this->assertSession(); + $assert->pageTextContains($this->teamA->label()); + $assert->pageTextNotContains($this->teamB->label()); + $this->drupalLogout(); + + // bMemberAccount should only see teamB. + $this->drupalLogin($this->bMemberAccount); + $this->queueCompaniesResponse($companies); + $this->queueDeveloperResponse($this->bMemberAccount, 200, ['companies' => [$this->teamB->id()]]); + $this->drupalGet(Url::fromUserInput('/teams')); + $assert = $this->assertSession(); + $assert->pageTextNotContains($this->teamA->label()); + $assert->pageTextContains($this->teamB->label()); + $this->drupalLogout(); + + // cMemberAccount should not see any teams. + $this->drupalLogin($this->cMemberAccount); + $this->queueCompaniesResponse($companies); + $this->queueDeveloperResponse($this->cMemberAccount); + $this->queueDeveloperResponse($this->cMemberAccount); + $this->drupalGet(Url::fromUserInput('/teams')); + $assert = $this->assertSession(); + $assert->pageTextNotContains($this->teamA->label()); + $assert->pageTextNotContains($this->teamB->label()); + + // Give cMemberAccount permission to view all teams. + $this->cMemberAccount->addRole($this->customRole); + $this->cMemberAccount->save(); + + // cMemberAccount should see both teams now. + $this->queueCompaniesResponse($companies); + $this->drupalGet(Url::fromUserInput('/teams')); + $assert = $this->assertSession(); + $assert->pageTextContains($this->teamA->label()); + $assert->pageTextContains($this->teamB->label()); } /** @@ -194,15 +211,22 @@ protected function getRenderedList() { * @return \Drupal\Core\Entity\EntityInterface * The user account. */ - protected function createAccount() { - $account = $this->entityTypeManager->getStorage('user')->create([ - 'mail' => $this->randomMachineName() . '@example.com', - 'name' => $this->randomMachineName(), - 'first_name' => $this->getRandomGenerator()->word(16), - 'last_name' => $this->getRandomGenerator()->word(16), - ]); + protected function createNewAccount() { + $this->disableUserPresave(); + $account = $this->createAccount(); + + $fields = [ + 'email' => $account->getEmail(), + 'userName' => $account->getAccountName(), + 'firstName' => $this->getRandomGenerator()->word(8), + 'lastName' => $this->getRandomGenerator()->word(8), + ]; + + // Stack developer responses for "created" and "set active". $this->queueDeveloperResponse($account, Response::HTTP_CREATED); - $account->save(); + $this->stack->queueMockResponse('no_content'); + $developer = Developer::create($fields); + $developer->save(); return $account; } 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 2aab3c35..6d719d82 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 @@ -210,6 +210,21 @@ protected function queueCompanyResponse(Company $company, $response_code = NULL) $this->stack->queueMockResponse(['company' => $context]); } + /** + * Queues up a mock companies response. + * + * @param array $companies + * An array of company objects. + * @param string|null $response_code + * Add a response code to override the default. + */ + protected function queueCompaniesResponse(array $companies, $response_code = NULL) { + $context = empty($response_code) ? [] : ['status_code' => $response_code]; + $context['companies'] = $companies; + + $this->stack->queueMockResponse(['companies' => $context]); + } + /** * Queues up a mock developers in a company response. * @@ -260,10 +275,10 @@ protected function createDeveloperApp(): DeveloperAppInterface { * * @throws \Drupal\Core\Entity\EntityStorageException */ - protected function createTeam(): TeamInterface {static $i = 0; + protected function createTeam(): TeamInterface { /** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $team */ $team = Team::create([ - 'name' => $this->randomMachineName() . '_' . $i++, + 'name' => $this->randomMachineName(), 'displayName' => $this->randomGenerator->name(), ]); $this->queueCompanyResponse($team->decorated()); @@ -288,14 +303,17 @@ protected function createTeam(): TeamInterface {static $i = 0; * The developer entity. */ public function addUserToTeam(TeamInterface $team, UserInterface $user) { - $context['developer'] = $user; - $context['org_name'] = $this->sdkConnector->getOrganization(); - $context['companies'] = [$team->id()]; + $this->queueDevsInCompanyResponse([ + ['email' => $user->getEmail()], + ]); + $this->queueCompanyResponse($team->decorated()); - $this->stack->queueMockResponse(['get_developer' => $context]); + $teamMembershipManager = \Drupal::service('apigee_edge_teams.team_membership_manager'); + $teamMembershipManager->addMembers($team->id(), [$user->getEmail()]); - $teams = \Drupal::service('apigee_edge_teams.team_membership_manager')->getTeams($user->getEmail()); - static::assertSame([$team->id()], $teams); + $this->queueDeveloperResponse($user, 200, [ + 'companies' => [$team->id()], + ]); return $this->entityTypeManager->getStorage('developer')->load($user->getEmail()); } From b71a1e0c87bb4f8d8c7898521c6e3dede50048d9 Mon Sep 17 00:00:00 2001 From: Arlina Espinoza Date: Thu, 11 Jun 2020 14:04:07 -0700 Subject: [PATCH 5/7] [#408] Fix team lists cache errors - fixing phpcs. --- .../tests/src/Functional/TeamListBuilderTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/apigee_edge_teams/tests/src/Functional/TeamListBuilderTest.php b/modules/apigee_edge_teams/tests/src/Functional/TeamListBuilderTest.php index 8b61eb6e..e357bfbc 100644 --- a/modules/apigee_edge_teams/tests/src/Functional/TeamListBuilderTest.php +++ b/modules/apigee_edge_teams/tests/src/Functional/TeamListBuilderTest.php @@ -100,7 +100,11 @@ class TeamListBuilderTest extends ApigeeEdgeTeamsFunctionalTestBase { */ protected $teamB; - + /** + * A role. + * + * @var \Drupal\user\Entity\Role + */ protected $customRole; /** From f91c03b787d694d48a4d1e6c71c4e0cfbff1fd78 Mon Sep 17 00:00:00 2001 From: Arlina Espinoza Date: Sun, 21 Jun 2020 23:10:09 -0700 Subject: [PATCH 6/7] [#408] Fix tests. --- .../Kernel/ApigeeEdgeActionsRulesKernelTestBase.php | 2 +- .../Plugin/RulesEvent/EdgeEntityAddMemberEventTest.php | 6 +----- .../RulesEvent/EdgeEntityRemoveMemberEventTest.php | 10 ++++------ .../src/Traits/ApigeeMockApiClientHelperTrait.php | 1 + 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/modules/apigee_edge_actions/tests/src/Kernel/ApigeeEdgeActionsRulesKernelTestBase.php b/modules/apigee_edge_actions/tests/src/Kernel/ApigeeEdgeActionsRulesKernelTestBase.php index 5f8744c1..2757444b 100644 --- a/modules/apigee_edge_actions/tests/src/Kernel/ApigeeEdgeActionsRulesKernelTestBase.php +++ b/modules/apigee_edge_actions/tests/src/Kernel/ApigeeEdgeActionsRulesKernelTestBase.php @@ -86,8 +86,8 @@ protected function setUp() { 'first_name' => $this->getRandomGenerator()->word(16), 'last_name' => $this->getRandomGenerator()->word(16), ]); - $this->account->save(); $this->queueDeveloperResponse($this->account, Response::HTTP_CREATED); + $this->account->save(); } /** diff --git a/modules/apigee_edge_actions/tests/src/Kernel/Plugin/RulesEvent/EdgeEntityAddMemberEventTest.php b/modules/apigee_edge_actions/tests/src/Kernel/Plugin/RulesEvent/EdgeEntityAddMemberEventTest.php index d61f1ff1..1e4e0455 100644 --- a/modules/apigee_edge_actions/tests/src/Kernel/Plugin/RulesEvent/EdgeEntityAddMemberEventTest.php +++ b/modules/apigee_edge_actions/tests/src/Kernel/Plugin/RulesEvent/EdgeEntityAddMemberEventTest.php @@ -82,11 +82,7 @@ public function testEvent() { $team = $this->createTeam(); // Add team member. - $this->queueCompanyResponse($team->decorated()); - $this->queueDeveloperResponse($this->account); - $this->container->get('apigee_edge_teams.team_membership_manager')->addMembers($team->id(), [ - $this->account->getEmail(), - ]); + $this->addUserToTeam($team, $this->account); $this->assertLogsContains("Event apigee_edge_actions_entity_add_member:team was dispatched."); $this->assertLogsContains("Member {$this->account->first_name->value} was added to team {$team->getDisplayName()}."); diff --git a/modules/apigee_edge_actions/tests/src/Kernel/Plugin/RulesEvent/EdgeEntityRemoveMemberEventTest.php b/modules/apigee_edge_actions/tests/src/Kernel/Plugin/RulesEvent/EdgeEntityRemoveMemberEventTest.php index 62387ec7..4ccbcedc 100644 --- a/modules/apigee_edge_actions/tests/src/Kernel/Plugin/RulesEvent/EdgeEntityRemoveMemberEventTest.php +++ b/modules/apigee_edge_actions/tests/src/Kernel/Plugin/RulesEvent/EdgeEntityRemoveMemberEventTest.php @@ -80,15 +80,13 @@ public function testEvent() { // Create a new team. $team = $this->createTeam(); - // Add team member. - $this->queueCompanyResponse($team->decorated()); - $this->queueDeveloperResponse($this->account); $team_membership_manager = $this->container->get('apigee_edge_teams.team_membership_manager'); - $team_membership_manager->addMembers($team->id(), [ - $this->account->getEmail(), - ]); + + // Add team member. + $this->addUserToTeam($team, $this->account); // Remove team member. + $this->stack->queueMockResponse('no_content'); $team_membership_manager->removeMembers($team->id(), [ $this->account->getEmail(), ]); 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 6d719d82..74640ac5 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 @@ -253,6 +253,7 @@ protected function createDeveloperApp(): DeveloperAppInterface { static $appId; $appId = $appId ? $appId++ : 1; + $this->queueDeveloperResponse($this->account); /** @var \Drupal\apigee_edge\Entity\DeveloperAppInterface $entity */ $entity = DeveloperApp::create([ 'appId' => $this->integration_enabled ? NULL : $appId, From 413787a20e5057550220f34b50cb69e737d9db51 Mon Sep 17 00:00:00 2001 From: Arlina Espinoza Date: Wed, 24 Jun 2020 12:25:53 -0700 Subject: [PATCH 7/7] [#408] Fix tests. --- .../src/Kernel/Entity/ListBuilder/EntityListBuilderTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/src/Kernel/Entity/ListBuilder/EntityListBuilderTest.php b/tests/src/Kernel/Entity/ListBuilder/EntityListBuilderTest.php index 050261b2..af705e92 100644 --- a/tests/src/Kernel/Entity/ListBuilder/EntityListBuilderTest.php +++ b/tests/src/Kernel/Entity/ListBuilder/EntityListBuilderTest.php @@ -89,6 +89,8 @@ protected function setUp() { $this->apigeeTestHelperSetup(); + $this->addOrganizationMatchedResponse(); + $this->account = User::create([ 'mail' => $this->randomMachineName() . '@example.com', 'name' => $this->randomMachineName(), @@ -96,7 +98,6 @@ protected function setUp() { 'last_name' => $this->getRandomGenerator()->word(16), ]); $this->account->save(); - $this->queueDeveloperResponse($this->account, Response::HTTP_CREATED); } /** @@ -134,10 +135,7 @@ protected function tearDown() { public function testDisplaySettings() { /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ $entity_type_manager = $this->container->get('entity_type.manager'); - $this->queueDeveloperResponse($this->account); $this->app = $this->createDeveloperApp(); - - $this->queueDeveloperResponse($this->account); $this->stack->queueMockResponse([ 'get_developer_apps' => [ 'apps' => [$this->app]