Skip to content
Snippets Groups Projects
Commit e0069a76 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3432368 by alexpott, daniel.bosen: Use the new validate method when...

Issue #3432368 by alexpott, daniel.bosen: Use the new validate method when processing the queue to decide whether it is worth sending to VG Wort
parent f336230d
No related branches found
Tags 2.0.0-beta11
1 merge request!45Use validate() when processing a message
Pipeline #129475 passed
......@@ -36,7 +36,7 @@ class EntityInfo {
return;
}
$api_message = $this->apiMessageGenerator->entityToNewMessage($entity, FALSE);
$api_message = $this->apiMessageGenerator->entityToNewMessage($entity);
$warnings = $api_message->validate();
if (empty($warnings)) {
return;
......
<?php
namespace Drupal\vgwort\Exception;
class NoParticipantsException extends NewMessageException {
/**
* {@inheritdoc}
*/
public function retries(): int {
return 6;
}
}
......@@ -10,10 +10,8 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\vgwort\Api\MessageText;
use Drupal\vgwort\Api\NewMessage;
use Drupal\vgwort\Api\Participant;
use Drupal\vgwort\Api\Webrange;
use Drupal\vgwort\Exception\NoCounterIdException;
use Drupal\vgwort\Exception\NoParticipantsException;
/**
* Converts entities to VG Wort messages.
......@@ -34,16 +32,20 @@ class MessageGenerator {
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity to create a new message object for.
* @param bool $check_has_author
* Whether to throw an exception if there is no author. Defaults to TRUE.
*
* @return \Drupal\vgwort\Api\NewMessage
* The new message object representing the entity.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
* Throw if the entity does not have a vgwort_counter_id field.
* Throw if the entity cannot generate a URL because it does not have an
* entity ID.
* @throws \Drupal\vgwort\Exception\NewMessageException
* Throw if the entity cannot be converted to a
* \Drupal\vgwort\Api\NewMessage object. Note, that the message represented
* by the object might not be valid for VG Wort. Call NewMessage::validate()
* to check validity.
*/
public function entityToNewMessage(FieldableEntityInterface $entity, bool $check_has_author = TRUE): NewMessage {
public function entityToNewMessage(FieldableEntityInterface $entity): NewMessage {
if (!$entity->hasField('vgwort_counter_id') || $entity->vgwort_counter_id->isEmpty()) {
throw new NoCounterIdException('Entities must have the vgwort_counter_id in order to generate a VG Wort new message notification');
}
......@@ -58,9 +60,6 @@ class MessageGenerator {
(string) $this->renderer->renderPlain($build)
);
$participants = $this->participantListManager->getParticipants($entity);
if ($check_has_author && !Participant::listHasAuthor($participants)) {
throw new NoParticipantsException('Entities must have at least one author in order to generate a VG Wort new message notification');
}
$webranges = [new Webrange([$entity->toUrl()->setAbsolute()->toString()])];
$legal_rights = $this->config->get('legal_rights');
......
......@@ -100,11 +100,17 @@ class RegistrationNotification extends JobTypeBase implements ContainerFactoryPl
try {
$message = $this->messageGenerator->entityToNewMessage($entity);
$errors = $message->validate();
}
catch (NewMessageException $e) {
$errors = [$e->getMessage()];
$retries = $e->retries();
}
if (!empty($errors)) {
return JobResult::failure(
sprintf('The entity %s:%s failed: %s', $entity_type, $entity_id, $e->getMessage()),
$e->retries(),
sprintf('The entity %s:%s failed: %s', $entity_type, $entity_id, implode(' ', $errors)),
$retries ?? 6,
// As this is failing before sending wait for another 2 weeks before
// trying again. This gives the site time to update things like author
// information.
......@@ -112,6 +118,9 @@ class RegistrationNotification extends JobTypeBase implements ContainerFactoryPl
);
}
// Assertion to keep PHPStan happy.
assert(isset($message), '$message must be set if we get here');
$headers = [
'Authorization' => 'Basic ' . base64_encode($this->config->get('username') . ':' . $this->config->get('password')),
'Content-Type' => 'application/json',
......
......@@ -170,7 +170,7 @@ class EntityQueueTest extends VgWortKernelTestBase {
'surname' => 'Jones',
'agency_abbr' => '',
],
'text' => 'Some text',
'text' => $this->getRandomGenerator()->paragraphs(30),
'name' => 'A title',
]);
$entity->save();
......@@ -182,7 +182,7 @@ class EntityQueueTest extends VgWortKernelTestBase {
'surname' => 'Jones',
'agency_abbr' => '',
],
'text' => 'Some text',
'text' => $this->getRandomGenerator()->paragraphs(30),
'name' => 'A title',
]);
$another_entity->save();
......@@ -215,7 +215,7 @@ class EntityQueueTest extends VgWortKernelTestBase {
// Having a new counter ID will cause a new row to be added to the map and
// the old still to be processed row to be removed.
$another_entity->set('text', 'Edited text 2')->save();
$another_entity->set('text', $this->getRandomGenerator()->paragraphs(30))->save();
$jobs = self::JOB_COUNT;
$jobs[Job::STATE_QUEUED] = '1';
$jobs[Job::STATE_SUCCESS] = '1';
......@@ -224,7 +224,7 @@ class EntityQueueTest extends VgWortKernelTestBase {
// Re-saving a successfully set entity will add a new row to be processed
// because the counter ID has changed.
$entity->setNewRevision();
$entity->set('text', 'Edited text again')->save();
$entity->set('text', $this->getRandomGenerator()->paragraphs(30))->save();
$jobs[Job::STATE_QUEUED] = '2';
$jobs[Job::STATE_SUCCESS] = '1';
$this->assertSame($jobs, $queue->getBackend()->countJobs());
......@@ -299,7 +299,7 @@ class EntityQueueTest extends VgWortKernelTestBase {
'surname' => 'Jones',
'agency_abbr' => '',
],
'text' => 'Some text',
'text' => $this->getRandomGenerator()->paragraphs(30),
'name' => 'A title',
]);
$entity->save();
......@@ -310,7 +310,7 @@ class EntityQueueTest extends VgWortKernelTestBase {
'surname' => 'Jones',
'agency_abbr' => '',
],
'text' => 'More text',
'text' => $this->getRandomGenerator()->paragraphs(30),
'name' => 'Another title',
]);
$another_entity->save();
......@@ -437,7 +437,7 @@ class EntityQueueTest extends VgWortKernelTestBase {
->getStorage(static::ENTITY_TYPE);
/** @var \Drupal\entity_test\Entity\EntityTestRevPub $entity */
$entity = $entity_storage->create([
'text' => 'Some text',
'text' => $this->getRandomGenerator()->paragraphs(30),
'name' => 'A title',
'user_id' => $user->id(),
]);
......@@ -457,7 +457,7 @@ class EntityQueueTest extends VgWortKernelTestBase {
$this->container->get('advancedqueue.processor')->processQueue($queue);
$this->assertSame($jobs, $queue_backend->countJobs());
$job = $this->container->get('vgwort.entity_job_mapper')->getJob($entity);
$this->assertSame('The entity entity_test_revpub:1 failed: Entities must have at least one author in order to generate a VG Wort new message notification', $job->getMessage());
$this->assertSame('The entity entity_test_revpub:1 failed: In order to be counted by VG Wort there must be at least one author.', $job->getMessage());
$this->assertSame('1', $job->getNumRetries());
// Go 30 days into the future.
......@@ -465,7 +465,7 @@ class EntityQueueTest extends VgWortKernelTestBase {
$this->container->get('advancedqueue.processor')->processQueue($queue);
$this->assertSame($jobs, $queue_backend->countJobs());
$job = $this->container->get('vgwort.entity_job_mapper')->getJob($entity);
$this->assertSame('The entity entity_test_revpub:1 failed: Entities must have at least one author in order to generate a VG Wort new message notification', $job->getMessage());
$this->assertSame('The entity entity_test_revpub:1 failed: In order to be counted by VG Wort there must be at least one author.', $job->getMessage());
$this->assertSame('2', $job->getNumRetries());
// Add VG Wort info to user.
......
......@@ -6,7 +6,6 @@ use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Tests\vgwort\Traits\PrettyJsonTrait;
use Drupal\vgwort\Exception\NoCounterIdException;
use Drupal\vgwort\Exception\NoParticipantsException;
/**
* Tests the vgwort message generator service.
......@@ -38,24 +37,24 @@ class MessageGeneratorTest extends VgWortKernelTestBase {
$this->container->get('vgwort.message_generator')->entityToNewMessage($entity);
}
public function testNoParticipantException() {
public function testNoParticipantValidation() {
$entity_type = 'entity_test';
$entity = $this->container->get('entity_type.manager')
->getStorage($entity_type)
->create([
'text' => 'Some text',
'text' => $this->getRandomGenerator()->paragraphs(30),
'name' => 'A title',
]);
$entity->save();
/** @var \Drupal\vgwort\ParticipantListManager $participant_manager */
$participant_manager = $this->container->get('vgwort.participant_list_manager');
$this->assertCount(0, $participant_manager->getParticipants($entity));
$this->expectExceptionMessage('Entities must have at least one author in order to generate a VG Wort new message notification');
$this->expectException(NoParticipantsException::class);
$this->container->get('vgwort.message_generator')->entityToNewMessage($entity);
$message = $this->container->get('vgwort.message_generator')->entityToNewMessage($entity);
$errors = array_map('strval', $message->validate());
$this->assertSame(['In order to be counted by VG Wort there must be at least one author.'], $errors);
}
public function testNoAuthorsNoParticipantException() {
public function testNoAuthorsNoParticipantValidation() {
$entity_type = 'entity_test';
$entity = $this->container->get('entity_type.manager')
->getStorage($entity_type)
......@@ -67,7 +66,7 @@ class MessageGeneratorTest extends VgWortKernelTestBase {
'surname' => 'Jones',
'agency_abbr' => '',
],
'text' => 'Some text',
'text' => $this->getRandomGenerator()->paragraphs(30),
'name' => 'A title',
]);
$entity->save();
......@@ -76,9 +75,35 @@ class MessageGeneratorTest extends VgWortKernelTestBase {
$participant_manager = $this->container->get('vgwort.participant_list_manager');
$this->assertCount(1, $participant_manager->getParticipants($entity));
$this->expectExceptionMessage('Entities must have at least one author in order to generate a VG Wort new message notification');
$this->expectException(NoParticipantsException::class);
$this->container->get('vgwort.message_generator')->entityToNewMessage($entity);
$message = $this->container->get('vgwort.message_generator')->entityToNewMessage($entity);
$errors = array_map('strval', $message->validate());
$this->assertSame(['In order to be counted by VG Wort there must be at least one author.'], $errors);
}
public function testEntityMessageLengthValidation() {
$entity_type = 'entity_test';
$entity = $this->container->get('entity_type.manager')
->getStorage($entity_type)
->create([
'vgwort_test' => [
'card_number' => '123123123',
'firstname' => 'Bob',
'surname' => 'Jones',
'agency_abbr' => '',
],
'text' => 'Some text',
'name' => 'A title',
]);
$entity->save();
$message = $this->container->get('vgwort.message_generator')->entityToNewMessage($entity);
$errors = array_map('strval', $message->validate());
$this->assertSame(['The minimum numbers of characters in order to be counted by VG Wort is 1800. The current count is 30.'], $errors);
$entity->text = $this->getRandomGenerator()->paragraphs(30);
$entity->save();
$message = $this->container->get('vgwort.message_generator')->entityToNewMessage($entity);
$this->assertEmpty($message->validate());
}
public function testEntityToNewMessage() {
......
......@@ -78,7 +78,7 @@ class RegistrationNotificationJobTypeTest extends VgWortKernelTestBase {
'surname' => 'Jones',
'agency_abbr' => '',
],
'text' => 'Some text',
'text' => $this->getRandomGenerator()->paragraphs(30),
'name' => 'A title',
]);
$entity->save();
......
......@@ -72,7 +72,7 @@ abstract class VgWortKernelTestBase extends KernelTestBase {
FieldStorageConfig::create([
'field_name' => 'text',
'entity_type' => static::ENTITY_TYPE,
'type' => 'string',
'type' => 'string_long',
])->save();
FieldConfig::create([
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment