View source
<?php
namespace Drupal\Tests\apigee_edge_teams\FunctionalJavascript;
use Drupal\apigee_edge\Entity\ApiProductInterface;
use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverWebAssert;
use Drupal\Tests\apigee_edge\FunctionalJavascript\ApigeeEdgeFunctionalJavascriptTestBase;
class ManageTeamAppsApiProductAccessTest extends ApigeeEdgeFunctionalJavascriptTestBase {
protected static $modules = [
'apigee_edge_teams',
];
protected $account;
protected $team;
protected $teamApp;
protected $publicProduct;
protected $privateProduct;
protected $internalProduct;
protected function setUp() {
parent::setUp();
$this
->config('apigee_edge_teams.team_settings')
->set('non_member_team_apps_visible_api_products', [
'private',
])
->save();
$this->account = $this
->createAccount([
'manage team apps',
]);
$apiProductStorage = $this->container
->get('entity_type.manager')
->getStorage('api_product');
$api_product = $apiProductStorage
->create([
'name' => $this
->randomMachineName(),
'displayName' => $this
->randomMachineName() . " (public)",
'approvalType' => ApiProductInterface::APPROVAL_TYPE_AUTO,
]);
$api_product
->setAttribute('access', 'public');
$api_product
->save();
$this->publicProduct = $api_product;
$api_product = $apiProductStorage
->create([
'name' => $this
->randomMachineName(),
'displayName' => $this
->randomMachineName() . " (private)",
'approvalType' => ApiProductInterface::APPROVAL_TYPE_AUTO,
]);
$api_product
->setAttribute('access', 'private');
$api_product
->save();
$this->privateProduct = $api_product;
$api_product = $apiProductStorage
->create([
'name' => $this
->randomMachineName(),
'displayName' => $this
->randomMachineName() . " (internal)",
'approvalType' => ApiProductInterface::APPROVAL_TYPE_AUTO,
]);
$api_product
->setAttribute('access', 'internal');
$api_product
->save();
$this->internalProduct = $api_product;
$teamStorage = $this->container
->get('entity_type.manager')
->getStorage('team');
$teamName = strtolower($this
->randomMachineName());
$team = $teamStorage
->create([
'name' => $teamName,
'displayName' => $teamName,
]);
$team
->save();
$this->team = $team;
$teamAppStorage = $this->container
->get('entity_type.manager')
->getStorage('team_app');
$teamApp = $teamAppStorage
->create([
'name' => $this
->randomMachineName(),
'companyName' => $this->team
->getName(),
]);
$teamApp
->save();
$this->teamApp = $teamApp;
$teamAppCredentialControllerFactory = $this->container
->get('apigee_edge_teams.controller.team_app_credential_controller_factory');
$credentialController = $teamAppCredentialControllerFactory
->teamAppCredentialController($this->team
->id(), $this->teamApp
->getName());
$credentials = $this->teamApp
->getCredentials();
$credential = reset($credentials);
$credentialController
->addProducts($credential
->getConsumerKey(), [
$this->publicProduct
->id(),
$this->privateProduct
->id(),
]);
}
protected function tearDown() {
if ($this->account !== NULL) {
try {
$this->account
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
if ($this->team !== NULL) {
try {
$this->team
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
if ($this->publicProduct !== NULL) {
try {
$this->publicProduct
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
if ($this->privateProduct !== NULL) {
try {
$this->privateProduct
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
if ($this->internalProduct !== NULL) {
try {
$this->internalProduct
->delete();
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
parent::tearDown();
}
public function testManageTeamAppsApiProductAccess() {
$assert_session = $this
->assertSession();
$message = 'You are not member of this team. You may see APIs here that a team member can not see.';
$verifyApiProductAccessOnAddForm = function (WebDriverWebAssert $assert_session, string $message) {
$this
->assertSession()
->pageTextContains($this->privateProduct
->label());
$this
->assertSession()
->pageTextNotContains($this->publicProduct
->label());
$this
->assertSession()
->pageTextNotContains($this->internalProduct
->label());
};
$this
->drupalLogin($this->account);
$this
->drupalGet($this->teamApp
->toUrl('add-form'));
$verifyApiProductAccessOnAddForm($assert_session, $message);
$assert_session
->selectExists('Owner')
->selectOption($this->team
->id());
$assert_session
->assertWaitOnAjaxRequest(1200000);
$this
->assertSession()
->pageTextContains($message);
$this
->drupalGet(Url::fromRoute('entity.team_app.add_form_for_team', [
'team' => $this->team
->id(),
]));
$verifyApiProductAccessOnAddForm($assert_session, $message);
$this
->assertSession()
->pageTextContains($message);
$this
->drupalGet($this->teamApp
->toUrl('edit-form'));
$this
->assertSession()
->pageTextContains($this->privateProduct
->label());
$this
->assertSession()
->pageTextContains($this->publicProduct
->label());
$this
->assertSession()
->pageTextNotContains($this->internalProduct
->label());
$this
->assertSession()
->pageTextContains($message);
}
}