View source
<?php
namespace Drupal\Tests\apigee_edge_teams\Functional;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Tests\apigee_edge\Traits\EntityUtilsTrait;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
class UiTest extends ApigeeEdgeTeamsFunctionalTestBase {
use EntityUtilsTrait;
use FieldUiTestTrait;
protected static $modules = [
'field_ui',
];
protected $teamStorage;
protected $account;
protected $otherAccount;
protected $team;
protected $product;
protected $fields;
protected function setUp() {
parent::setUp();
$this->teamStorage = $this->container
->get('entity_type.manager')
->getStorage('team');
$this
->installExtraModules([
'block',
]);
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('local_actions_block');
$this->product = $this
->createProduct();
$this->account = $this
->createAccount([
'administer team',
]);
$this->otherAccount = $this
->createAccount();
$this
->addFieldsToEntities();
}
protected function tearDown() {
if ($this->account !== NULL) {
try {
$this->account
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
if ($this->otherAccount !== NULL) {
try {
$this->otherAccount
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
if ($this->team !== NULL) {
try {
$this->teamStorage
->delete([
$this->team,
]);
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
if ($this->product !== NULL) {
try {
$this->product
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
parent::tearDown();
}
public function testUi() {
$this
->teamsWorkflowTest();
$this
->teamAndTeamAppLabelTest();
}
protected function teamsWorkflowTest() {
$this
->drupalLogin($this->account);
$this
->drupalGet(Url::fromRoute('entity.team.collection'));
$this
->clickLink('Add team');
$team_name = $team_display_name = mb_strtolower($this
->getRandomGenerator()
->name());
$this
->submitForm([
'name' => $team_name,
'displayName[0][value]' => $team_display_name,
'field_integer[0][value]' => $this->fields['integer']['data'],
'field_email[0][value]' => $this->fields['email']['data'],
], 'Add team');
$this->team = $this->teamStorage
->load($team_display_name);
$this
->clickLink($team_display_name);
$this
->assertSession()
->pageTextContains($team_display_name);
$this
->assertSession()
->pageTextContains($this->fields['integer']['data']);
$this
->assertSession()
->pageTextContains($this->fields['email']['data']);
$this
->clickLink('Edit');
$team_modified_display_name = $this
->randomMachineName();
$this
->submitForm([
'displayName[0][value]' => $team_modified_display_name,
'field_integer[0][value]' => $this->fields['integer']['data_edited'],
'field_email[0][value]' => $this->fields['email']['data_edited'],
], 'Save team');
$this
->clickLink($team_modified_display_name);
$this
->assertSession()
->pageTextContains($team_modified_display_name);
$this
->assertSession()
->pageTextContains($this->fields['integer']['data_edited']);
$this
->assertSession()
->pageTextContains($this->fields['email']['data_edited']);
$teamMembershipManager = $this->container
->get('apigee_edge_teams.team_membership_manager');
$teamMembershipManager
->addMembers($this->team
->id(), [
$this->otherAccount
->getEmail(),
]);
$this
->drupalPostForm(Url::fromRoute('apigee_edge_teams.settings.team.permissions'), [
'member[team_manage_members]' => TRUE,
'member[team_app_create]' => TRUE,
'member[team_app_update]' => TRUE,
'member[team_app_delete]' => TRUE,
'member[team_app_analytics]' => TRUE,
], 'Save permissions');
$this
->drupalLogin($this->otherAccount);
$this
->drupalGet($this->team
->toUrl('collection'));
$this
->clickLink($team_modified_display_name);
$this
->clickLink('Team Apps');
$this
->assertSession()
->pageTextContains('There are no team apps yet.');
$this
->clickLink('Add team app');
$team_app_1_name = $team_app_1_display_name = mb_strtolower($this
->getRandomGenerator()
->name());
$this
->submitForm([
'name' => $team_app_1_name,
'displayName[0][value]' => $team_app_1_display_name,
'field_integer[0][value]' => $this->fields['integer']['data'],
'field_email[0][value]' => $this->fields['email']['data'],
"api_products[{$this->product->getName()}]" => $this->product
->getName(),
], 'Add team app');
$this
->assertSession()
->pageTextContains('Team App has been successfully created.');
$this
->clickLink($team_app_1_display_name);
$this
->assertSession()
->pageTextContains($team_app_1_display_name);
$this
->assertSession()
->pageTextContains($this->fields['integer']['data']);
$this
->assertSession()
->pageTextContains($this->fields['email']['data']);
$this
->clickLink('Edit');
$team_app_1_modified_display_name = $this
->randomMachineName();
$this
->submitForm([
'displayName[0][value]' => $team_app_1_modified_display_name,
'field_integer[0][value]' => $this->fields['integer']['data_edited'],
'field_email[0][value]' => $this->fields['email']['data_edited'],
], 'Save');
$this
->clickLink($team_app_1_modified_display_name);
$this
->assertSession()
->pageTextContains($team_app_1_modified_display_name);
$this
->assertSession()
->pageTextContains($this->fields['integer']['data_edited']);
$this
->assertSession()
->pageTextContains($this->fields['email']['data_edited']);
$this
->clickLink('Analytics');
$this
->assertSession()
->pageTextContains('No performance data is available for the criteria you supplied.');
$this
->drupalLogin($this->account);
$this
->drupalGet(Url::fromRoute('entity.team_app.collection'));
$this
->assertSession()
->linkExists($team_modified_display_name);
$this
->assertSession()
->linkExists($team_app_1_modified_display_name);
$this
->clickLink($team_app_1_modified_display_name);
$this
->clickLink('Delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('The name does not match the team app you are attempting to delete.');
$this
->drupalLogin($this->account);
$this
->drupalGet(Url::fromRoute('entity.team_app.collection'));
$this
->clickLink($team_modified_display_name);
$this
->clickLink('Members');
$this
->getSession()
->getPage()
->findById(Html::getUniqueId($this->otherAccount
->getEmail()))
->clickLink('Remove');
$this
->submitForm([], 'Confirm');
$this
->drupalLogin($this->otherAccount);
$this
->drupalGet($this->team
->toUrl('collection'));
$this
->assertSession()
->pageTextContains('There are no Teams yet.');
$this
->drupalLogin($this->account);
$this
->drupalGet($this->team
->toUrl('collection'));
$this
->clickLink($team_modified_display_name);
$this
->clickLink('Delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('The name does not match the team you are attempting to delete.');
$this
->submitForm([
'verification_code' => $team_name,
], 'Delete');
$this
->assertSession()
->pageTextContains("The {$team_modified_display_name} team has been deleted.");
$this
->assertSession()
->linkNotExists($team_modified_display_name);
$this
->drupalLogin($this->otherAccount);
$this
->drupalGet($this->team
->toUrl('collection'));
$this
->assertSession()
->pageTextContains('There are no Teams yet.');
}
protected function teamAndTeamAppLabelTest() {
$this
->drupalLogin($this->rootUser);
$this
->changeEntityAliasesAndValidate('team', 'apigee_edge_teams.settings.team');
$this
->changeEntityAliasesAndValidate('team_app', 'apigee_edge_teams.settings.team_app');
}
protected function addFieldsToEntities() {
$this
->drupalLogin($this->rootUser);
$this->fields = [
'integer' => [
'type' => 'integer',
'data' => rand(),
'data_edited' => rand(),
],
'email' => [
'type' => 'email',
'data' => $this
->randomMachineName() . '@example.com',
'data_edited' => $this
->randomMachineName() . '@example.com',
],
];
$add_field_paths = [
Url::fromRoute('apigee_edge_teams.settings.team')
->toString(),
Url::fromRoute('apigee_edge_teams.settings.team_app')
->toString(),
];
foreach ($add_field_paths as $add_field_path) {
foreach ($this->fields as $name => $data) {
$this
->fieldUIAddNewField($add_field_path, $name, mb_strtoupper($name), $data['type'], ($data['settings'] ?? []) + [
'cardinality' => -1,
], []);
}
}
}
}