View source
<?php
namespace Drupal\Tests\apigee_edge\Functional;
use Apigee\Edge\Api\Management\Controller\DeveloperAppController;
use Apigee\Edge\Api\Management\Entity\App;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge\Entity\DeveloperApp;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Url;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Symfony\Component\HttpFoundation\Response;
class DeveloperAppFieldTest extends ApigeeEdgeFunctionalTestBase {
use FieldUiTestTrait;
protected static $modules = [
'link',
'block',
'field_ui',
];
protected $account;
protected $developer;
protected $developerApp;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this->account = $this
->createAccount([
'administer apigee edge',
'administer developer_app',
'administer display modes',
'administer developer_app fields',
'administer developer_app form display',
'administer developer_app display',
]);
$this->developer = Developer::load($this->account
->getEmail());
$this->developerApp = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => App::STATUS_APPROVED,
'developerId' => $this->developer
->getDeveloperId(),
]);
$this->developerApp
->setOwner($this->account);
$this->developerApp
->save();
$this
->drupalLogin($this->account);
}
protected function tearDown() {
try {
if ($this->account !== NULL) {
$this->account
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
parent::tearDown();
}
public function testFieldableDeveloperApp() {
$this
->fieldStorageFormattersTest();
$this
->typesTest();
$this
->requiredFieldTest();
$this
->formRegionTest();
$this
->viewRegionTest();
$this
->credentialsViewTest();
}
protected function fieldStorageFormattersTest() {
$field_name_prefix = (string) $this
->config('field_ui.settings')
->get('field_prefix');
$paragraph = trim($this
->getRandomGenerator()
->paragraphs(1));
$paragraphs = trim($this
->getRandomGenerator()
->paragraphs());
$link = [
[
'uri' => 'http://example.com',
'title' => 'Example',
'options' => [],
'attributes' => [],
],
];
$fields = [
strtolower($this
->randomMachineName()) => [
'type' => 'boolean',
'data' => [
[
'value' => TRUE,
],
],
'encoded' => '1',
],
strtolower($this
->randomMachineName()) => [
'type' => 'float',
'data' => [
[
'value' => M_PI,
],
[
'value' => M_E,
],
[
'value' => M_EULER,
],
],
'encoded' => implode(',', [
M_PI,
M_E,
M_EULER,
]),
],
strtolower($this
->randomMachineName()) => [
'type' => 'integer',
'data' => [
[
'value' => 4,
],
[
'value' => 9,
],
],
'encoded' => '4,9',
],
strtolower($this
->randomMachineName()) => [
'type' => 'decimal',
'data' => [
[
'value' => '0.1',
],
],
'encoded' => '0.1',
],
strtolower($this
->randomMachineName()) => [
'type' => 'list_float',
'settings' => [
'settings[allowed_values]' => implode(PHP_EOL, [
M_PI,
M_E,
M_EULER,
]),
],
'data' => [
[
'value' => M_PI,
],
],
'encoded' => (string) M_PI,
],
strtolower($this
->randomMachineName()) => [
'type' => 'list_integer',
'settings' => [
'settings[allowed_values]' => implode(PHP_EOL, [
1,
2,
3,
]),
],
'data' => [
[
'value' => 2,
],
[
'value' => 3,
],
],
'encoded' => '2,3',
],
strtolower($this
->randomMachineName()) => [
'type' => 'list_string',
'settings' => [
'settings[allowed_values]' => implode(PHP_EOL, [
'qwer',
'asdf',
'zxcv',
]),
],
'data' => [
[
'value' => 'qwer',
],
[
'value' => 'asdf',
],
[
'value' => 'zxcv',
],
],
'encoded' => 'qwer,asdf,zxcv',
],
strtolower($this
->randomMachineName()) => [
'type' => 'string',
'data' => [
[
'value' => $paragraph,
],
],
'encoded' => "\"{$paragraph}\"",
],
strtolower($this
->randomMachineName()) => [
'type' => 'string_long',
'data' => [
[
'value' => $paragraphs,
],
],
'encoded' => "\"{$paragraphs}\"",
],
strtolower($this
->randomMachineName()) => [
'type' => 'email',
'data' => [
[
'value' => 'test@example.com',
],
[
'value' => 'test_2@example.com',
],
],
'encoded' => 'test@example.com,test_2@example.com',
],
strtolower($this
->randomMachineName()) => [
'type' => 'timestamp',
'data' => [
[
'value' => 1531212177,
],
[
'value' => 1531234234,
],
],
'encoded' => '1531212177,1531234234',
],
strtolower($this
->randomMachineName()) => [
'type' => 'link',
'data' => $link,
'encoded' => json_encode($link),
],
];
$add_field_path = Url::fromRoute('apigee_edge.settings.developer_app')
->toString();
foreach ($fields as $name => $data) {
$this
->fieldUIAddNewField($add_field_path, $name, mb_strtoupper($name), $data['type'], ($data['settings'] ?? []) + [
'cardinality' => -1,
], []);
}
drupal_flush_all_caches();
$this->developerApp = DeveloperApp::load($this->developerApp
->id());
foreach ($fields as $name => $data) {
$full_field_name = "{$field_name_prefix}{$name}";
$this->developerApp
->set($full_field_name, $data['data']);
}
$this->developerApp
->save();
$loaded_app = DeveloperApp::load($this->developerApp
->id());
$connector = $this->container
->get('apigee_edge.sdk_connector');
$controller = new DeveloperAppController($connector
->getOrganization(), $this->developer
->getDeveloperId(), $connector
->getClient());
$rawLoadedApp = $controller
->load($this->developerApp
->getName());
foreach ($fields as $name => $data) {
$full_field_name = "{$field_name_prefix}{$name}";
$this
->assertEquals($data['data'], $loaded_app
->get($full_field_name)
->getValue());
$this
->assertEquals($data['encoded'], $rawLoadedApp
->getAttributeValue($name));
}
}
protected function typesTest() {
$field_values = [
'scopes' => [
'a',
'b',
'c',
],
'displayName' => $this
->getRandomGenerator()
->word(16),
];
foreach ($field_values as $field_name => $field_value) {
$this->developerApp
->set($field_name, $field_value);
}
$this->developerApp
->preSave(new class implements EntityStorageInterface {
public function resetCache(array $ids = NULL) {
}
public function loadMultiple(array $ids = NULL) {
}
public function load($id) {
}
public function loadUnchanged($id) {
}
public function loadRevision($revision_id) {
}
public function deleteRevision($revision_id) {
}
public function loadByProperties(array $values = []) {
}
public function create(array $values = []) {
}
public function delete(array $entities) {
}
public function save(EntityInterface $entity) {
}
public function hasData() {
}
public function getQuery($conjunction = 'AND') {
}
public function getAggregateQuery($conjunction = 'AND') {
}
public function getEntityTypeId() {
}
public function getEntityType() {
}
public function restore(EntityInterface $entity) {
}
});
foreach ($field_values as $field_name => $field_value) {
$getter = 'get' . ucfirst($field_name);
$value = call_user_func([
$this->developerApp,
$getter,
]);
if ($value instanceof \DateTimeImmutable) {
$value = $value
->getTimestamp();
}
$this
->assertEquals($field_value, $value);
}
}
protected function requiredFieldTest() {
$this
->submitBaseFieldConfigForm();
$this
->submitFormDisplay([
'callbackUrl' => 'hidden',
]);
$this
->submitBaseFieldConfigForm(TRUE, TRUE, FALSE);
$this
->submitFormDisplay([
'callbackUrl' => 'content',
]);
$this
->submitBaseFieldConfigForm(TRUE, TRUE);
$this
->submitFormDisplay([
'callbackUrl' => 'hidden',
], FALSE);
$this
->submitBaseFieldConfigForm(FALSE, FALSE);
}
protected function formRegionTest() {
$this
->assertFieldVisibleOnEntityForm('Callback URL');
$this
->submitFormDisplay([
'callbackUrl' => 'hidden',
]);
$this
->assertFieldVisibleOnEntityForm('Callback URL', FALSE);
$this
->submitFormDisplay([
'callbackUrl' => 'content',
]);
$this
->assertFieldVisibleOnEntityForm('Callback URL');
}
protected function viewRegionTest() {
$callbackUrl = 'https://' . strtolower($this
->randomMachineName()) . '.example.com';
$this->developerApp
->setCallbackUrl($callbackUrl);
$this->developerApp
->save();
$assert = function (bool $visible = TRUE) use ($callbackUrl) {
$this
->assertFieldVisibleOnEntityDisplay($this->developerApp
->getName(), 'Callback URL', $callbackUrl, $visible);
};
$this
->submitViewDisplay([
'callbackUrl' => 'content',
]);
$assert(TRUE);
$this
->submitViewDisplay([
'callbackUrl' => 'hidden',
]);
$assert(FALSE);
}
protected function credentialsViewTest() {
$assert = function (bool $visible = TRUE) {
$this
->assertFieldVisibleOnEntityDisplay($this->developerApp
->getName(), 'Credential', 'Key Status', $visible);
};
$this
->submitViewDisplay([
'credentials' => 'hidden',
]);
$assert(FALSE);
$this
->submitViewDisplay([
'credentials' => 'content',
]);
$assert(TRUE);
}
protected function submitBaseFieldConfigForm(bool $description_required = FALSE, bool $callback_url_required = FALSE, bool $expect_success = TRUE) {
$this
->drupalPostForm(Url::fromRoute('entity.developer_app.field_ui_fields'), [
'table[description][required]' => $description_required,
'table[callbackUrl][required]' => $callback_url_required,
], 'Save');
if ($expect_success) {
$this
->assertSession()
->pageTextContains('Field settings have been saved successfully.');
}
else {
$this
->assertSession()
->pageTextContains('is hidden on the default form display.');
}
}
protected function submitFormDisplay(array $region_overrides = [], bool $expect_success = TRUE) {
$edit = [];
foreach ($region_overrides as $field => $region) {
$edit["fields[{$field}][region]"] = $region;
}
$this
->drupalPostForm(Url::fromRoute('entity.entity_form_display.developer_app.default'), $edit, 'Save');
if ($expect_success) {
$this
->assertSession()
->pageTextContains('Your settings have been saved.');
}
else {
$this
->assertSession()
->pageTextContains('is required.');
}
}
protected function submitViewDisplay(array $region_overrides = []) {
$edit = [];
foreach ($region_overrides as $field => $region) {
$edit["fields[{$field}][region]"] = $region;
}
$this
->drupalPostForm(Url::fromRoute('entity.entity_view_display.developer_app.default'), $edit, 'Save');
$this
->assertSession()
->pageTextContains('Your settings have been saved.');
}
protected function assertFieldVisibleOnEntityForm(string $field_label, bool $visible = TRUE) {
$this
->drupalGet(Url::fromRoute('entity.developer_app.add_form_for_developer', [
'user' => $this->account
->id(),
]));
$this
->assertEquals(Response::HTTP_OK, $this
->getSession()
->getStatusCode());
if ($visible) {
$this
->assertSession()
->pageTextContains($field_label);
}
else {
$this
->assertSession()
->pageTextNotContains($field_label);
}
}
protected function assertFieldVisibleOnEntityDisplay(string $app_name, string $field_label, string $field_value, bool $visible = TRUE) {
$this
->drupalGet(Url::fromRoute('entity.developer_app.canonical_by_developer', [
'user' => $this->account
->id(),
'app' => $app_name,
]));
if ($visible) {
$this
->assertSession()
->pageTextContains($field_label);
$this
->assertSession()
->pageTextContains($field_value);
}
else {
$this
->assertSession()
->pageTextNotContains($field_label);
$this
->assertSession()
->pageTextNotContains($field_value);
}
}
}