Skip to content

Commit

Permalink
Merge pull request #236 from stooit/issues-225
Browse files Browse the repository at this point in the history
Issues 225
  • Loading branch information
steveworley authored Oct 13, 2020
2 parents bd52c60 + 3e4450b commit 80a8ccd
Show file tree
Hide file tree
Showing 27 changed files with 195 additions and 93 deletions.
Empty file added info.log
Empty file.
19 changes: 15 additions & 4 deletions modules/quant_api/src/Client/QuantClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
use Drupal\quant_api\Exception\InvalidPayload;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\MultipartStream;

/**
*
* Quant API client.
*/
class QuantClient implements QuantClientInterface {

Expand Down Expand Up @@ -82,14 +83,24 @@ public function ping() {
]);
}
catch (RequestException $e) {
\Drupal::messenger()->addError(t($e->getMessage()));
\Drupal::messenger()->addError($e->getMessage());
return FALSE;
}

if ($response->getStatusCode() == 200) {
return TRUE;
}

if ($response->getStatusCode() == 402) {
// Emit a subscription invalid warning.
\Drupal::messenger()->addError(t('Your Quant subscription is invalid. Please check the dashboard.'));
}

if ($response->getStatusCode() == 410) {
// Emit a deleted project warning.
\Drupal::messenger()->addError(t('Project is deleted. Please check the dashboard for restoration options.'));
}

return FALSE;
}

Expand Down Expand Up @@ -150,7 +161,7 @@ public function sendFile(string $file, string $url, int $rid = NULL) : array {
'POST',
$this->endpoint,
$headers,
new Psr7\MultipartStream([
new MultipartStream([
[
'name' => basename($file),
'filename' => basename($file),
Expand Down Expand Up @@ -180,7 +191,7 @@ public function unpublish(string $url) : array {
'Quant-Customer' => $this->username,
'Quant-Project' => $this->project,
'Quant-Token' => $this->token,
]
],
]);

return json_decode($response->getBody(), TRUE);
Expand Down
24 changes: 16 additions & 8 deletions modules/quant_api/src/EventSubscriber/QuantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class QuantApi implements EventSubscriberInterface {
*
* @param \Drupal\quant_api\Client\QuantClientInterface $client
* The Drupal HTTP Client to make requests.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger channel factory.
* @param \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher $event_dispatcher
* The event dispatcher.
*/
public function __construct(QuantClientInterface $client, LoggerChannelFactoryInterface $logger_factory, ContainerAwareEventDispatcher $event_dispatcher) {
$this->client = $client;
Expand Down Expand Up @@ -78,7 +82,7 @@ public function onRedirect(QuantRedirectEvent $event) {
$data = [
'url' => $source,
'redirect_url' => $dest,
'redirect_http_code' => (int)$statusCode,
'redirect_http_code' => (int) $statusCode,
'published' => TRUE,
];

Expand All @@ -92,7 +96,6 @@ public function onRedirect(QuantRedirectEvent $event) {
return $res;
}


/**
* Trigger an API request with the event data.
*
Expand All @@ -114,6 +117,10 @@ public function onOutput(QuantEvent $event) {
'proxy_override' => $meta['proxy_override'],
];

if (isset($meta['content_type'])) {
$data['headers']['content_type'] = $meta['content_type'];
}

if (!empty($rid = $event->getRevisionId())) {
$data['revision'] = $rid;
}
Expand Down Expand Up @@ -151,9 +158,9 @@ public function onOutput(QuantEvent $event) {
if (file_exists(DRUPAL_ROOT . $file)) {
$this->eventDispatcher->dispatch(QuantFileEvent::OUTPUT, new QuantFileEvent(DRUPAL_ROOT . $file, $file));
}
else if (strpos($url, '/styles/')) {
// Image style derivative does not exist.
// Quant API returns an expected full_path item which allows for image generation.
elseif (strpos($url, '/styles/')) {
// Image style derivative does not exist. Quant API returns an expected
// full_path item which allows for image generation.
if (isset($item['full_path'])) {
// Build internal request.
$config = \Drupal::config('quant.settings');
Expand Down Expand Up @@ -187,8 +194,8 @@ public function onOutput(QuantEvent $event) {

/** @var \DOMElement $node */
$pager_operations = [];
// @todo: Make this xpath configurable.
// This supports the use case for core views output (mini and standard pager).
// This supports the use case for core views (mini and standard pager).
// @TODO: selector should be configurable.
foreach ($xpath->query('//a[contains(@href,"page=") and (./span[contains(text(), "Next")])]') as $node) {
$original_href = $node->getAttribute('href');
if ($original_href[0] === '?') {
Expand All @@ -214,7 +221,6 @@ public function onOutput(QuantEvent $event) {
batch_set($batch);
}


// @todo: Report on forms that need proxying (attachments.forms).
}

Expand All @@ -234,9 +240,11 @@ public function onMedia(QuantFileEvent $event) {
}
catch (InvalidPayload $error) {
$this->logger->error($error->getMessage());
return;
}
catch (Exception $error) {
$this->logger->error($error->getMessage());
return;
}

return $res;
Expand Down
3 changes: 2 additions & 1 deletion modules/quant_api/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public function buildForm(array $form, FormStateInterface $form_state) {

if ($config->get('api_token')) {
if ($project = $this->client->ping()) {
\Drupal::messenger()->addMessage(t('Successfully connected to ' . $config->get('api_project')));
$message = t('Successfully connected to @api', ['@api' => $config->get('api_project')]);
\Drupal::messenger()->addMessage($message);
}
else {
\Drupal::messenger()->addError(t('Unable to connect to Quant API, check settings.'));
Expand Down
21 changes: 15 additions & 6 deletions modules/quant_api/tests/src/Unit/QuantClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\RequestOptions;
use Drupal\quant_api\Exception\InvalidPayload;

/**
* Ensure that the client responds correctly.
Expand All @@ -21,7 +20,8 @@ class QuantClientTest extends UnitTestCase {
/**
* Get a stubbed config factory.
*
* @return ConfigFactoryInterface
* @return \Drupal\Core\Config\ConfigFactoryInterface
* The config interface.
*/
protected function getConfigStub($default = []) {
$value = [
Expand All @@ -45,12 +45,13 @@ protected function getConfigStub($default = []) {
* Get a successful project response.
*
* @return GuzzleHttp\Psr7\Response
* A response object.
*/
protected function getProjectResponse() {
// @TODO - should these be fixtures.
$body = [
'project' => 'test',
'error' => false,
'error' => FALSE,
'errorMsg' => '',
];

Expand All @@ -65,6 +66,7 @@ protected function getProjectResponse() {
* A valid redirect response.
*
* @return GuzzleHttp\Psr7\Response
* A response object.
*/
protected function getRedirectResponse() {
$body = [
Expand All @@ -73,7 +75,7 @@ protected function getRedirectResponse() {
'url' => '/a',
'redirect_http_code' => 302,
'errorMsg' => '',
'error' => false,
'error' => FALSE,
];

$res = $this->prophesize(Response::class);
Expand All @@ -87,10 +89,11 @@ protected function getRedirectResponse() {
* Get an invalid response.
*
* @return GuzzleHttp\Psr7\Response
* A response object.
*/
protected function getInvalidResponse() {
$body = [
'error' => true,
'error' => TRUE,
'errorMsg' => 'Error',
];

Expand Down Expand Up @@ -239,7 +242,7 @@ public function testSendRedirectValid() {
'url' => '/a',
'redirect_http_code' => 302,
'errorMsg' => '',
'error' => false,
'error' => FALSE,
], $redirect);
}

Expand Down Expand Up @@ -272,7 +275,9 @@ public function testSendRedirectError() {
* @expectedException Drupal\quant_api\Exception\InvalidPayload
*/
public function testSendFileFileNoExist() {
// phpcs:ignore
global $exists_return;
// phpcs:ignore
global $readable_return;

$exists_return = FALSE;
Expand All @@ -290,7 +295,9 @@ public function testSendFileFileNoExist() {
* Ensure files are validated before sending.
*/
public function testSendFileValid() {
// phpcs:ignore
global $exists_return;
// phpcs:ignore
global $readable_return;

$exists_return = TRUE;
Expand Down Expand Up @@ -332,6 +339,7 @@ public function testSendFileValid() {
* Stub file_exists.
*/
function file_exists($path) {
// phpcs:ignore
global $exists_return;
if (isset($exists_return)) {
return $exists_return;
Expand All @@ -343,6 +351,7 @@ function file_exists($path) {
* Stub is_readable.
*/
function is_readable($path) {
// phpcs:ignore
global $readable_return;
if (isset($readable_return)) {
return $readable_return;
Expand Down
8 changes: 8 additions & 0 deletions modules/quant_cron/quant_cron.module
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

/**
* @file
* Add cron support for quant processing.
*/

use Drupal\quant\Seed;
use Drupal\Core\Form\FormState;
use Drupal\quant\Event\CollectEntitiesEvent;
Expand All @@ -8,6 +13,9 @@ use Drupal\quant\Event\CollectRedirectsEvent;
use Drupal\quant\Event\CollectRoutesEvent;
use Drupal\quant\Event\QuantCollectionEvents;

/**
* Implements hook_cron().
*/
function quant_cron_cron() {

// Quant cron only supported via CLI.
Expand Down
8 changes: 3 additions & 5 deletions modules/quant_cron/src/Form/CronSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Drupal\quant_cron\Form;

use Drupal\node\Entity\Node;
use Drupal\quant\Seed;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\quant\QuantStaticTrait;
Expand All @@ -18,14 +16,14 @@ class CronSettingsForm extends FormBase {
use QuantStaticTrait;

/**
* {@inheritdoc}.
* {@inheritdoc}
*/
public function getFormId() {
return 'quant_cron_settings_form';
}

/**
* {@inheritdoc}.
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {

Expand Down Expand Up @@ -91,7 +89,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
->loadMultiple();

$content_types = [];
foreach($types as $type) {
foreach ($types as $type) {
$content_types[$type->id()] = $type->label();
}

Expand Down
1 change: 1 addition & 0 deletions modules/quant_sitemap/quant_sitemap.module
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

/**
* @file
* Hook implementations for Quant sitemap.
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function getSubscribedEvents() {
* Get the simple sitemap manager.
*
* @return Drupal\simple_sitemap\SimplesitemapManager
* The sitemap manager.
*/
public function getSitemapManager() {
return \Drupal::service('simple_sitemap.manager');
Expand All @@ -56,6 +57,7 @@ public function getSitemapManager() {
* The entity type manager.
*
* @return Drupal\Core\Entity\EntityTypeManager
* The entity type manager.
*/
public function getEntityTypeManager() {
return $this->entityTypeManager;
Expand Down Expand Up @@ -111,7 +113,7 @@ public function collectRoutes(CollectRoutesEvent $event) {
if ($this->moduleHandler->moduleExists('simple_sitemap')) {
$items = $this->getSimpleSitemapItems();
}
else if ($this->moduleHandler->moduleExists('xmlsitemap')) {
elseif ($this->moduleHandler->moduleExists('xmlsitemap')) {
$items = $this->getXmlsitemapItems();
}

Expand Down
2 changes: 0 additions & 2 deletions quant.install
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Drupal\Core\Database\Database;

/**
* @file
* Install hook definitions for Quant.
Expand Down
Loading

0 comments on commit 80a8ccd

Please sign in to comment.