View source
<?php
namespace Drupal\Tests\apigee_edge_teams\Functional;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Response;
class TeamListBuilderTest extends ApigeeEdgeTeamsFunctionalTestBase {
protected static $mock_api_client_ready = TRUE;
protected static $modules = [
'system',
'user',
'options',
'key',
'apigee_edge',
'apigee_edge_teams',
'apigee_mock_api_client',
];
protected $teamStorage;
protected $account;
protected $aMemberAccount;
protected $bMemberAccount;
protected $cMemberAccount;
protected $teamA;
protected $teamB;
protected $customRole;
protected function setUp() {
parent::setUp();
$this
->addOrganizationMatchedResponse();
$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);
$this->account = $this->rootUser;
$this->aMemberAccount = $this
->createNewAccount();
$this->bMemberAccount = $this
->createNewAccount();
$this->cMemberAccount = $this
->createNewAccount();
$this->customRole = $this
->drupalCreateRole([
'view any team',
]);
$this->teamA = $this
->createTeam();
$this->teamB = $this
->createTeam();
$this
->addUserToTeam($this->teamA, $this->aMemberAccount);
$this
->addUserToTeam($this->teamB, $this->bMemberAccount);
}
protected function tearDown() {
try {
$this->teamStorage
->delete([
$this->teamA,
$this->teamB,
]);
$this->account
->delete();
$this->aMemberAccount
->delete();
$this->bMemberAccount
->delete();
$this->cMemberAccount
->delete();
} catch (\Error $error) {
} catch (\Exception $exception) {
}
}
public function testTeamListCache() {
$companies = [
$this->teamA
->decorated(),
$this->teamB
->decorated(),
];
$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();
$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();
$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());
$this->cMemberAccount
->addRole($this->customRole);
$this->cMemberAccount
->save();
$this
->queueCompaniesResponse($companies);
$this
->drupalGet(Url::fromUserInput('/teams'));
$assert = $this
->assertSession();
$assert
->pageTextContains($this->teamA
->label());
$assert
->pageTextContains($this->teamB
->label());
}
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),
];
$this
->queueDeveloperResponse($account, Response::HTTP_CREATED);
$this->stack
->queueMockResponse('no_content');
$developer = Developer::create($fields);
$developer
->save();
return $account;
}
}