Skip to content

Commit

Permalink
Add acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Jan 7, 2025
1 parent 2d279bc commit 2377fd9
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ adding:
- 'edit own sum_summer_courses content'
- 'revert stanford_page revisions'
- 'revert sum_summer_courses revisions'
- 'schedule publishing of summer_entity'
- 'use text format sum_markdown'
- 'view any unpublished sum_summer_courses content'
- 'view scheduled summer_entity'
- 'view sum_summer_courses revisions'
removing:
permissions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ adding:
- 'administer summer entities'
- 'configure all stanford_page node layout overrides'
- 'configure editable stanford_page node layout overrides'
- 'schedule publishing of summer_entity'
- 'use text format sum_markdown'
- 'view scheduled summer_entity'
removing:
permissions:
- 'edit stanford_global_message config page entity'
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ adding:
- 'administer summer entities'
- 'configure all stanford_page node layout overrides'
- 'configure editable stanford_page node layout overrides'
- 'schedule publishing of summer_entity'
- 'use text format sum_markdown'
- 'view scheduled summer_entity'
removing:
permissions:
- 'edit stanford_global_message config page entity'
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ adding:
- 'edit terms in sum_summer_courses'
- 'import sum_csv_summer_courses migration'
- 'revert sum_summer_courses revisions'
- 'schedule publishing of summer_entity'
- 'use text format sum_markdown'
- 'view any unpublished sum_summer_courses content'
- 'view scheduled summer_entity'
- 'view sum_summer_courses revisions'
removing:
dependencies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ adding:
- 'edit terms in sum_summer_courses'
- 'import sum_csv_summer_courses migration'
- 'revert sum_summer_courses revisions'
- 'schedule publishing of summer_entity'
- 'use text format sum_markdown'
- 'view any unpublished sum_summer_courses content'
- 'view scheduled summer_entity'
- 'view sum_summer_courses revisions'
removing:
dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
adding:
display:
default:
display_options:
access:
options:
perm: 'update any stanford_component_block block content'
removing:
display:
default:
display_options:
access:
options:
perm: 'edit any stanford_component_block block content'
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
Expand Down Expand Up @@ -32,119 +33,117 @@ public function __construct(protected EntityTypeManagerInterface $entityTypeMana
*/
public function validate(mixed $value, Constraint $constraint) {
/** @var \Drupal\summer_helper\SummerInterface $value */
$summer_storage = $this->entityTypeManager->getStorage('summer_entity');
$query = $summer_storage->getQuery()
->accessCheck(FALSE)
->condition('bundle', 'global_msg');

$query->condition($query->orConditionGroup()
->condition('status', TRUE)
->condition('publish_on', time(), '>'));
$published_message = $query->execute();

$publish_date = (int) $value->get('publish_on')->getString();
$unpublish_date = (int) $value->get('unpublish_on')->getString();

// When saving the current published message, no need to check the published
// dates.
if (!$publish_date && $published_message && $value->id() != reset($published_message)) {
// If no publish or unpublish date configured.
if (!$publish_date && !$unpublish_date && $this->isAnyPublished($value->id())) {
$this->context->addViolation($constraint->invalidatePublishedDate);
return;
}

// If both "published on" and "unpublished on" dates are configured, validate no other messages occur during that time.
if ($publish_date && $unpublish_date) {
$query = $summer_storage->getQuery()
->accessCheck(FALSE)
->condition('bundle', 'global_msg');
$query->condition($query->orConditionGroup()
->condition('publish_on', [$publish_date, $unpublish_date], 'between')
->condition('unpublish_on', [
$publish_date,
$unpublish_date,
], 'between'));

$ids = $query->execute();
if ($ids && (count($ids) > 1 || $value->id() != reset($ids))) {
$this->context->addViolation($constraint->invalidatePublishedDate);
}
// If published date is configured but not unpublished date.
if ($publish_date && !$unpublish_date && $this->isPublishDateValid($publish_date, $value->id())) {
$this->context->addViolation($constraint->invalidatePublishedDate);
return;
}

// If only the "published on" date is configured, validate no other messages will occur after.
if ($publish_date && !$unpublish_date) {
// Another message will be published in the future.
$query = $summer_storage->getQuery()
->accessCheck(FALSE)
->condition('bundle', 'global_msg');
$query->condition($query->orConditionGroup()
->condition('publish_on', $publish_date, '>')
->condition('unpublish_on', $publish_date, '>'));

$ids = $query->execute();
if ($ids && (count($ids) > 1 || $value->id() != reset($ids))) {
$this->context->addViolation($constraint->invalidatePublishedDate);
return;
}

// Another message is scheduled to publish before this one, but is not
// schedule to unpublish.
$query = $summer_storage->getQuery()
->accessCheck(FALSE)
->condition('bundle', 'global_msg')
->condition('publish_on', $publish_date, '<')
->condition('unpublish_on', NULL, 'IS NULL');
$ids = $query->execute();
if ($ids && (count($ids) > 1 || $value->id() != reset($ids))) {
$this->context->addViolation($constraint->invalidatePublishedDate);
return;
}

// Another message is currently published, but isn't schedule to be
// unpublished.
$query = $summer_storage->getQuery()
->accessCheck(FALSE)
->condition('bundle', 'global_msg')
->condition('status', TRUE)
->condition('unpublish_on', NULL, 'IS NULL');
$ids = $query->execute();
if ($ids && (count($ids) > 1 || $value->id() != reset($ids))) {
$this->context->addViolation($constraint->invalidatePublishedDate);
return;
}
// If unpublished date is configured but not published date.
if (!$publish_date && $unpublish_date && $this->isUnpublishDateValid($unpublish_date, $value->id())) {
$this->context->addViolation($constraint->invalidatePublishedDate);
return;
}

// If only the "unpublished on" date is configured, validate no other messages will occur until that date.
if (!$publish_date && $unpublish_date) {
$query = $summer_storage->getQuery()
->accessCheck(FALSE)
->condition('bundle', 'global_msg');
// If unpublished date is configured but not published date.
if ($publish_date && $unpublish_date && $this->isBothDatesValid($publish_date, $unpublish_date, $value->id())) {
$this->context->addViolation($constraint->invalidatePublishedDate);
}
}

$query->condition($query->orConditionGroup()
->condition('publish_on', $unpublish_date, '<')
->condition('status', TRUE));
$ids = $query->execute();
protected function isAnyPublished($exclude_id = NULL): bool {
$query = $this->getEntityQuery($exclude_id);
$condition_group = $query->orConditionGroup();
$condition_group->condition('status', TRUE)
->condition('publish_on', NULL, 'IS NOT NULL');

if ($ids && (count($ids) > 1 || $value->id() != reset($ids))) {
$this->context->addViolation($constraint->invalidatePublishedDate);
}
return;
}
$query->condition($condition_group);
return !empty($query->execute());
}

$query = $summer_storage->getQuery()
->accessCheck(FALSE)
->condition('bundle', 'global_msg');
protected function isPublishDateValid($publish_date, $exclude_id = NULL): bool {
$query = $this->getEntityQuery($exclude_id);

// A message is currently published and won't be unpublished.
$first_condition_group = $query->andConditionGroup()
->condition('status', TRUE)
->condition('unpublish_on', NULL, 'IS NULL');

// A message is unpublished and will be published after the published date.
$second_condition_group = $query->orConditionGroup()
->condition('publish_on', $publish_date, '>')
->condition('unpublish_on', $publish_date, '>');

$query->condition($query->orConditionGroup()
->condition($first_condition_group)
->condition($second_condition_group));
return !empty($query->execute());
}

protected function isUnpublishDateValid($unpublish_date, $exclude_id = NULL): bool {
$query = $this->getEntityQuery($exclude_id);

// A message is currently published.
$first_condition_group = $query->andConditionGroup()
->condition('status', TRUE);

// A message will be published before the unpublish date.
$second_condition_group = $query->andConditionGroup()
->condition('publish_on', $unpublish_date, '<');

$query->condition($query->orConditionGroup()
->condition($first_condition_group)
->condition($second_condition_group));

return !empty($query->execute());
}

protected function isBothDatesValid($publish_date, $unpublish_date, $exclude_id = NULL): bool {
$query = $this->getEntityQuery($exclude_id);
// A message is currently published and won't be unpublished.
$first_condition_group = $query->andConditionGroup()
->condition('status', TRUE)
->condition('publish_on', time(), '>'));
$ids = $query->execute();
->condition('unpublish_on', NULL, 'IS NULL');

// When saving the current published message, no need to check the published
// dates.
if ($ids && (count($ids) > 1 || $value->id() != reset($ids))) {
$this->context->addViolation($constraint->invalidatePublishedDate);
// A message will be published after the published date.
$second_condition_group = $query->andConditionGroup()
->condition('publish_on', $publish_date, '>')
->condition($query->orConditionGroup()
->condition('unpublish_on', $unpublish_date, '<')
->condition('unpublish_on', NULL, 'IS NULL'));

// A message will be published before the unpublish date.
$third_condition_group = $query->andConditionGroup()
->condition('publish_on', $publish_date, '>')
->condition('publish_on', $unpublish_date, '<');

$query->condition($query->orConditionGroup()
->condition($first_condition_group)
->condition($second_condition_group)
->condition($third_condition_group));

return !empty($query->execute());
}

protected function getEntityQuery($exclude_id = NULL): QueryInterface {
$summer_storage = $this->entityTypeManager->getStorage('summer_entity');
$query = $summer_storage->getQuery()
->accessCheck(FALSE)
->condition('bundle', 'global_msg');

if ($exclude_id) {
$query->condition('id', $exclude_id, '!=');
}
return $query;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function setUp(): void {
$query->method('accessCheck')->willReturnSelf();
$query->method('condition')->willReturnSelf();
$query->method('orConditionGroup')->willReturn($condition_group);
$query->method('andConditionGroup')->willReturn($condition_group);
$query->method('execute')->willReturnReference($this->entityQueryResults);

$entity_storage = $this->createMock(EntityStorageInterface::class);
Expand All @@ -70,6 +71,7 @@ protected function setUp(): void {
$this->validator->initialize($this->getContext());

$this->messageEntity = $this->createMock(SummerInterface::class);
$this->messageEntity->method('id')->willReturn(999);
$this->messageEntity->method('get')
->will($this->returnCallback([$this, 'getMessageField']));
}
Expand Down
Loading

0 comments on commit 2377fd9

Please sign in to comment.