|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2020 Google Inc. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify it under |
| 7 | + * the terms of the GNU General Public License version 2 as published by the |
| 8 | + * Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 13 | + * License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License along |
| 16 | + * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | + */ |
| 19 | + |
| 20 | +namespace Drupal\Tests\apigee_edge_teams\Functional; |
| 21 | + |
| 22 | +use Drupal\apigee_edge\Entity\Developer; |
| 23 | +use Drupal\Core\Url; |
| 24 | +use Symfony\Component\HttpFoundation\Response; |
| 25 | + |
| 26 | +/** |
| 27 | + * Apigee Edge Teams list builder tests. |
| 28 | + * |
| 29 | + * @group apigee_edge |
| 30 | + * @group apigee_edge_teams |
| 31 | + */ |
| 32 | +class TeamListBuilderTest extends ApigeeEdgeTeamsFunctionalTestBase { |
| 33 | + |
| 34 | + /** |
| 35 | + * Indicates this test class is mock API client ready. |
| 36 | + * |
| 37 | + * @var bool |
| 38 | + */ |
| 39 | + protected static $mock_api_client_ready = TRUE; |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + protected static $modules = [ |
| 45 | + 'system', |
| 46 | + 'user', |
| 47 | + 'options', |
| 48 | + 'key', |
| 49 | + 'apigee_edge', |
| 50 | + 'apigee_edge_teams', |
| 51 | + 'apigee_mock_api_client', |
| 52 | + ]; |
| 53 | + |
| 54 | + /** |
| 55 | + * The team entity storage. |
| 56 | + * |
| 57 | + * @var \Drupal\apigee_edge_teams\Entity\Storage\TeamStorageInterface |
| 58 | + */ |
| 59 | + protected $teamStorage; |
| 60 | + |
| 61 | + /** |
| 62 | + * The user 1 account. |
| 63 | + * |
| 64 | + * @var \Drupal\user\UserInterface |
| 65 | + */ |
| 66 | + protected $account; |
| 67 | + |
| 68 | + /** |
| 69 | + * Drupal user who is a member team A. |
| 70 | + * |
| 71 | + * @var \Drupal\user\UserInterface |
| 72 | + */ |
| 73 | + protected $aMemberAccount; |
| 74 | + |
| 75 | + /** |
| 76 | + * Drupal user who is a member team B. |
| 77 | + * |
| 78 | + * @var \Drupal\user\UserInterface |
| 79 | + */ |
| 80 | + protected $bMemberAccount; |
| 81 | + |
| 82 | + /** |
| 83 | + * Drupal user who is an admin. |
| 84 | + * |
| 85 | + * @var \Drupal\user\UserInterface |
| 86 | + */ |
| 87 | + protected $cMemberAccount; |
| 88 | + |
| 89 | + /** |
| 90 | + * Team A entity to test. |
| 91 | + * |
| 92 | + * @var \Drupal\apigee_edge_teams\Entity\TeamInterface |
| 93 | + */ |
| 94 | + protected $teamA; |
| 95 | + |
| 96 | + /** |
| 97 | + * Team B entity to test. |
| 98 | + * |
| 99 | + * @var \Drupal\apigee_edge_teams\Entity\TeamInterface |
| 100 | + */ |
| 101 | + protected $teamB; |
| 102 | + |
| 103 | + /** |
| 104 | + * A role. |
| 105 | + * |
| 106 | + * @var \Drupal\user\Entity\Role |
| 107 | + */ |
| 108 | + protected $customRole; |
| 109 | + |
| 110 | + /** |
| 111 | + * {@inheritdoc} |
| 112 | + */ |
| 113 | + protected function setUp() { |
| 114 | + parent::setUp(); |
| 115 | + |
| 116 | + $this->addOrganizationMatchedResponse(); |
| 117 | + |
| 118 | + $this->teamStorage = $this->entityTypeManager->getStorage('team'); |
| 119 | + |
| 120 | + $config_factory = \Drupal::configFactory(); |
| 121 | + $config = $config_factory->getEditable('apigee_edge_teams.team_settings'); |
| 122 | + $config->set('cache_expiration', 300); |
| 123 | + $config->save(TRUE); |
| 124 | + |
| 125 | + // Create accounts: user 1, for members of two teams, and an extra one. |
| 126 | + $this->account = $this->rootUser; |
| 127 | + $this->aMemberAccount = $this->createNewAccount(); |
| 128 | + $this->bMemberAccount = $this->createNewAccount(); |
| 129 | + $this->cMemberAccount = $this->createNewAccount(); |
| 130 | + |
| 131 | + $this->customRole = $this->drupalCreateRole(['view any team']); |
| 132 | + |
| 133 | + // Create teams. |
| 134 | + $this->teamA = $this->createTeam(); |
| 135 | + $this->teamB = $this->createTeam(); |
| 136 | + |
| 137 | + // Add accounts to teams. |
| 138 | + $this->addUserToTeam($this->teamA, $this->aMemberAccount); |
| 139 | + $this->addUserToTeam($this->teamB, $this->bMemberAccount); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * {@inheritdoc} |
| 144 | + */ |
| 145 | + protected function tearDown() { |
| 146 | + try { |
| 147 | + $this->teamStorage->delete([$this->teamA, $this->teamB]); |
| 148 | + $this->account->delete(); |
| 149 | + $this->aMemberAccount->delete(); |
| 150 | + $this->bMemberAccount->delete(); |
| 151 | + $this->cMemberAccount->delete(); |
| 152 | + } |
| 153 | + catch (\Error $error) { |
| 154 | + // Do nothing. |
| 155 | + } |
| 156 | + catch (\Exception $exception) { |
| 157 | + // Do nothing. |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Tests team list cache. |
| 163 | + */ |
| 164 | + public function testTeamListCache() { |
| 165 | + $companies = [ |
| 166 | + $this->teamA->decorated(), |
| 167 | + $this->teamB->decorated(), |
| 168 | + ]; |
| 169 | + |
| 170 | + // aMemberAccount should only see teamA. |
| 171 | + $this->drupalLogin($this->aMemberAccount); |
| 172 | + $this->queueCompaniesResponse($companies); |
| 173 | + $this->queueDeveloperResponse($this->aMemberAccount, 200, ['companies' => [$this->teamA->id()]]); |
| 174 | + $this->drupalGet(Url::fromRoute('entity.team.collection')); |
| 175 | + $assert = $this->assertSession(); |
| 176 | + $assert->pageTextContains($this->teamA->label()); |
| 177 | + $assert->pageTextNotContains($this->teamB->label()); |
| 178 | + $this->drupalLogout(); |
| 179 | + |
| 180 | + // bMemberAccount should only see teamB. |
| 181 | + $this->drupalLogin($this->bMemberAccount); |
| 182 | + $this->queueCompaniesResponse($companies); |
| 183 | + $this->queueDeveloperResponse($this->bMemberAccount, 200, ['companies' => [$this->teamB->id()]]); |
| 184 | + $this->drupalGet(Url::fromUserInput('/teams')); |
| 185 | + $assert = $this->assertSession(); |
| 186 | + $assert->pageTextNotContains($this->teamA->label()); |
| 187 | + $assert->pageTextContains($this->teamB->label()); |
| 188 | + $this->drupalLogout(); |
| 189 | + |
| 190 | + // cMemberAccount should not see any teams. |
| 191 | + $this->drupalLogin($this->cMemberAccount); |
| 192 | + $this->queueCompaniesResponse($companies); |
| 193 | + $this->queueDeveloperResponse($this->cMemberAccount); |
| 194 | + $this->queueDeveloperResponse($this->cMemberAccount); |
| 195 | + $this->drupalGet(Url::fromUserInput('/teams')); |
| 196 | + $assert = $this->assertSession(); |
| 197 | + $assert->pageTextNotContains($this->teamA->label()); |
| 198 | + $assert->pageTextNotContains($this->teamB->label()); |
| 199 | + |
| 200 | + // Give cMemberAccount permission to view all teams. |
| 201 | + $this->cMemberAccount->addRole($this->customRole); |
| 202 | + $this->cMemberAccount->save(); |
| 203 | + |
| 204 | + // cMemberAccount should see both teams now. |
| 205 | + $this->queueCompaniesResponse($companies); |
| 206 | + $this->drupalGet(Url::fromUserInput('/teams')); |
| 207 | + $assert = $this->assertSession(); |
| 208 | + $assert->pageTextContains($this->teamA->label()); |
| 209 | + $assert->pageTextContains($this->teamB->label()); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Helper function to create a random user account. |
| 214 | + * |
| 215 | + * @return \Drupal\Core\Entity\EntityInterface |
| 216 | + * The user account. |
| 217 | + */ |
| 218 | + protected function createNewAccount() { |
| 219 | + $this->disableUserPresave(); |
| 220 | + $account = $this->createAccount(); |
| 221 | + |
| 222 | + $fields = [ |
| 223 | + 'email' => $account->getEmail(), |
| 224 | + 'userName' => $account->getAccountName(), |
| 225 | + 'firstName' => $this->getRandomGenerator()->word(8), |
| 226 | + 'lastName' => $this->getRandomGenerator()->word(8), |
| 227 | + ]; |
| 228 | + |
| 229 | + // Stack developer responses for "created" and "set active". |
| 230 | + $this->queueDeveloperResponse($account, Response::HTTP_CREATED); |
| 231 | + $this->stack->queueMockResponse('no_content'); |
| 232 | + $developer = Developer::create($fields); |
| 233 | + $developer->save(); |
| 234 | + |
| 235 | + return $account; |
| 236 | + } |
| 237 | + |
| 238 | +} |
0 commit comments