View source
<?php
namespace Drupal\Tests\apigee_edge\Functional;
use Apigee\Edge\Api\Management\Entity\App;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge\Entity\DeveloperApp;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Url;
class DeveloperAppAnalyticsTest extends ApigeeEdgeFunctionalTestBase {
protected static $mock_api_client_ready = TRUE;
protected $account;
protected $developer;
protected $developerApp;
protected $queryParameters;
protected function setUp() {
parent::setUp();
$this->account = $this
->createAccount([
'analytics own developer_app',
'analytics any developer_app',
]);
$this
->queueDeveloperResponse($this->account, 200);
$this->developer = Developer::load($this->account
->getEmail());
$this->developerApp = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'displayName' => $this
->randomMachineName(),
'status' => App::STATUS_APPROVED,
'developerId' => $this->developer
->getDeveloperId(),
]);
$this->developerApp
->setOwner($this->account);
$this
->queueDeveloperAppResponse($this->developerApp, 201);
$this->developerApp
->save();
$since = new DrupalDateTime();
$until = new DrupalDateTime();
$this->queryParameters = [
'query' => [
'metric' => 'min(total_response_time)',
'since' => $since
->sub(new \DateInterval('P3D'))
->getTimestamp(),
'until' => $until
->sub(new \DateInterval('P2D'))
->getTimestamp(),
'environment' => 'prod',
],
];
}
protected function tearDown() {
try {
if ($this->developer !== NULL) {
$this->developer
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
parent::tearDown();
}
public function testAnalytics() {
$this
->drupalLogin($this->account);
$path = Url::fromRoute('entity.developer_app.analytics_for_developer', [
'user' => $this->account
->id(),
'app' => $this->developerApp
->getName(),
])
->toString();
$this
->queueDeveloperAppResponse($this->developerApp);
$this
->visitAnalyticsPage($path);
$this
->visitAnalyticsPage($path, TRUE);
$path = Url::fromRoute('entity.developer_app.analytics', [
'developer_app' => $this->developerApp
->id(),
])
->toString();
$this
->visitAnalyticsPage($path);
$this
->visitAnalyticsPage($path, TRUE);
$this
->exportAnalyticsTest();
}
protected function visitAnalyticsPage(string $path, bool $appendQueryParameters = FALSE) {
$this
->queueAppAnalyticsStackedResponse();
if ($appendQueryParameters) {
$this
->drupalGet($path, $this->queryParameters);
}
else {
$this
->drupalGet($path);
}
$this
->assertAnalyticsPage();
$this
->assertSession()
->pageTextNotContains('Invalid URL query parameters.');
$since_in_the_future = new DrupalDateTime();
$since_in_the_future
->add(new \DateInterval('P3D'));
$until = new DrupalDateTime();
$this
->queueAppAnalyticsStackedResponse();
$this
->drupalGet($path, [
'query' => [
'metric' => 'sum(message_count)',
'since' => $since_in_the_future
->getTimestamp(),
'until' => $until
->getTimestamp(),
'environment' => 'prod',
],
]);
$this
->assertAnalyticsPage();
$this
->assertSession()
->pageTextContains('The end date cannot be before the start date.');
$until = new DrupalDateTime();
$this
->queueAppAnalyticsStackedResponse();
$this
->drupalGet($path, [
'query' => [
'metric' => 'sum(message_count)',
'since' => $since_in_the_future
->getTimestamp(),
'until' => $until
->add(new \DateInterval('P4D'))
->getTimestamp(),
'environment' => 'prod',
],
]);
$this
->assertAnalyticsPage();
$this
->assertSession()
->pageTextContains('Start date cannot be in future. The current local time of the Developer Portal:');
$this
->queueAppAnalyticsStackedResponse();
$this
->drupalGet($path, [
'query' => [
'metric' => $this
->randomMachineName(),
'since' => $this
->randomMachineName(),
'until' => $this
->randomMachineName(),
'environment' => 'prod',
],
]);
$this
->assertAnalyticsPage();
$this
->assertSession()
->pageTextContains('Invalid parameter metric in the URL.');
$this
->queueAppAnalyticsStackedResponse();
$this
->drupalGet($path, [
'query' => [
'metric' => 'min(total_response_time)',
'since' => $this
->randomMachineName(),
'until' => $this
->randomMachineName(),
'environment' => 'prod',
],
]);
$this
->assertAnalyticsPage();
$this
->assertSession()
->pageTextContains('Invalid URL query parameters.');
$this
->queueAppAnalyticsStackedResponse();
$this
->drupalGet($path, [
'query' => [
'metric' => 'min(total_response_time)',
'since' => (new DrupalDateTime())
->getTimestamp(),
'until' => (new DrupalDateTime())
->getTimestamp(),
'environment' => $this
->randomMachineName(),
],
]);
$this
->assertAnalyticsPage();
$this
->assertSession()
->pageTextContains('Invalid parameter environment in the URL.');
}
protected function assertAnalyticsPage() {
$timezone = date_default_timezone_get();
$this
->assertSession()
->pageTextContains("Analytics of {$this->developerApp->label()}");
$this
->assertSession()
->pageTextContains("Your timezone: {$timezone}");
$this
->assertSession()
->pageTextContains('No performance data is available for the criteria you supplied.');
$this
->assertSession()
->pageTextNotContains('Export CSV');
}
protected function exportAnalyticsTest() {
$this
->drupalLogin($this->rootUser);
$data_id = Crypt::randomBytesBase64();
$this
->drupalGet(Url::fromRoute('apigee_edge.export_analytics.csv', [
'data_id' => $data_id,
]));
$this
->assertEquals(403, $this
->getSession()
->getStatusCode());
$store = $this->container
->get('tempstore.private')
->get('apigee_edge.analytics');
$store
->set($data_id = Crypt::randomBytesBase64(), []);
$this
->drupalGet(Url::fromRoute('apigee_edge.export_analytics.csv', [
'data_id' => $data_id,
]));
$this
->assertEquals(403, $this
->getSession()
->getStatusCode());
}
protected function queueAppAnalyticsStackedResponse() {
$this->stack
->queueMockResponse([
'app_analytics' => [],
]);
}
}