View source
<?php
namespace Drupal\Tests\apigee_edge_teams\Functional;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge_teams\Entity\TeamRoleInterface;
use Drupal\Core\Url;
use Drupal\views\Views;
class TeamInvitationsTest extends ApigeeEdgeTeamsFunctionalTestBase {
protected static $mock_api_client_ready = TRUE;
protected static $modules = [
'user',
'options',
'datetime',
'views',
'apigee_edge_teams',
'apigee_mock_api_client',
];
protected $accountAdmin;
protected $accountUser;
protected $teamA;
protected $teamB;
protected function setUp() {
parent::setUp();
$this
->addOrganizationMatchedResponse();
$this->teamA = $this
->createTeam();
$this->teamB = $this
->createTeam();
$this->accountAdmin = $this
->createAccount([
'administer team',
]);
$this->accountUser = $this
->createAccount([
'accept own team invitation',
]);
if (!$this->integration_enabled) {
$this
->queueDeveloperResponse($this->accountAdmin);
Developer::load($this->accountAdmin
->getEmail());
$this
->queueDeveloperResponse($this->accountUser);
Developer::load($this->accountUser
->getEmail());
}
}
protected function tearDown() {
if (!$this->integration_enabled) {
return;
}
$teams = [
$this->teamA,
$this->teamB,
];
foreach ($teams as $team) {
if ($team !== NULL) {
try {
$team
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
}
$accounts = [
$this->accountAdmin,
$this->accountUser,
];
foreach ($accounts as $account) {
if ($account !== NULL) {
try {
$account
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
}
parent::tearDown();
}
public function testMultipleInvitations() {
$this
->drupalLogin($this->accountAdmin);
$teams = [
$this->teamA,
$this->teamB,
];
$companies = [
$this->teamA
->decorated(),
$this->teamB
->decorated(),
];
$inCache = FALSE;
foreach ($teams as $team) {
if (!$inCache) {
$this
->queueCompanyResponse($team
->decorated());
}
$this
->drupalGet(Url::fromRoute('entity.team.add_members', [
'team' => $team
->id(),
]));
$this
->assertSession()
->pageTextContains('Invite members');
$this
->queueDevsInCompanyResponse([]);
$this
->queueCompaniesResponse($companies);
$this
->queueCompaniesResponse($companies);
$this
->submitForm([
'developers' => $this->accountUser
->getEmail(),
], 'Invite members');
$successMessage = t('The following developer has been invited to the @team @team_label: @developer.', [
'@developer' => $this->accountUser
->getEmail(),
'@team' => $team
->label(),
'@team_label' => mb_strtolower($team
->getEntityType()
->getSingularLabel()),
]);
$this
->assertSession()
->pageTextContains($successMessage);
$inCache = TRUE;
}
}
public function testInvitationsList() {
$this
->assertNotNull(Views::getView('team_invitations'));
$teamInvitationStorage = $this->entityTypeManager
->getStorage('team_invitation');
$selected_roles = [
TeamRoleInterface::TEAM_MEMBER_ROLE => TeamRoleInterface::TEAM_MEMBER_ROLE,
];
$teams = [
$this->teamA,
$this->teamB,
];
$companies = [
$this->teamA
->decorated(),
$this->teamB
->decorated(),
];
foreach ($teams as $team) {
$this
->queueCompanyResponse($team
->decorated());
$teamInvitationStorage
->create([
'team' => [
'target_id' => $team
->id(),
],
'team_roles' => array_values(array_map(function (string $role) {
return [
'target_id' => $role,
];
}, $selected_roles)),
'recipient' => $this->accountUser
->getEmail(),
])
->save();
}
$this
->drupalLogin($this->accountUser);
$invitationsUrl = Url::fromRoute('view.team_invitations.user', [
'user' => $this->accountUser
->id(),
]);
$this
->queueCompaniesResponse($companies);
$this
->drupalGet($invitationsUrl);
foreach ($teams as $team) {
$this
->assertSession()
->pageTextContains('Invitation to join ' . $team
->label());
}
$this
->queueCompanyResponse($this->teamA
->decorated());
$teamALabel = $this->teamA
->label();
$this->teamA
->delete();
$this
->queueCompaniesResponse($companies);
$this
->queueCompanyResponse($this->teamB
->decorated());
$this
->drupalGet($invitationsUrl);
$this
->assertSession()
->pageTextNotContains('Invitation to join ' . $teamALabel);
$this
->assertSession()
->pageTextContains('Invitation to join ' . $this->teamB
->label());
}
}