Browse Source

Jsonld alter caching (#747)

* Invalidating cache appropriately when context that affect jsonld change

* Tests, coding standards
pull/750/head
dannylamb 5 years ago committed by Jared Whiklo
parent
commit
b553f2cd2b
  1. 11
      islandora.module
  2. 8
      tests/src/Functional/JsonldSelfReferenceReactionTest.php
  3. 4
      tests/src/Functional/JsonldTypeAlterReactionTest.php

11
islandora.module

@ -14,6 +14,7 @@
* @author Diego Pino Navarro <dpino@metro.org> https://github.com/diegopino * @author Diego Pino Navarro <dpino@metro.org> https://github.com/diegopino
*/ */
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
@ -253,6 +254,16 @@ function islandora_jsonld_alter_normalized_array(EntityInterface $entity, array
$context_manager = \Drupal::service('context.manager'); $context_manager = \Drupal::service('context.manager');
foreach ($context_manager->getActiveReactions('\Drupal\islandora\ContextReaction\NormalizerAlterReaction') as $reaction) { foreach ($context_manager->getActiveReactions('\Drupal\islandora\ContextReaction\NormalizerAlterReaction') as $reaction) {
$reaction->execute($entity, $normalized, $context); $reaction->execute($entity, $normalized, $context);
foreach ($context_manager->getActiveContexts() as $context_config) {
try {
if ($context_config->getReaction($reaction->getPluginId())) {
$context['cacheability']->addCacheTags($context_config->getCacheTags());
};
}
catch (PluginNotFoundException $e) {
// Squash :(.
}
}
} }
} }

8
tests/src/Functional/JsonldSelfReferenceReactionTest.php

@ -98,6 +98,10 @@ class JsonldSelfReferenceReactionTest extends IslandoraFunctionalTestBase {
$this->assertSession() $this->assertSession()
->pageTextContains("The context $context_name has been saved"); ->pageTextContains("The context $context_name has been saved");
// The first time a Context is saved, you need to clear the cache.
// Subsequent changes to the context don't need a cache rebuild, though.
drupal_flush_all_caches();
$new_contents = $this->drupalGet($url . '?_format=jsonld'); $new_contents = $this->drupalGet($url . '?_format=jsonld');
$json = \GuzzleHttp\json_decode($new_contents, TRUE); $json = \GuzzleHttp\json_decode($new_contents, TRUE);
$this->assertEquals( $this->assertEquals(
@ -177,6 +181,10 @@ class JsonldSelfReferenceReactionTest extends IslandoraFunctionalTestBase {
$this->assertSession() $this->assertSession()
->pageTextContains("The context $context_name has been saved"); ->pageTextContains("The context $context_name has been saved");
// The first time a Context is saved, you need to clear the cache.
// Subsequent changes to the context don't need a cache rebuild, though.
drupal_flush_all_caches();
$new_contents = $this->drupalGet($media_url . '?_format=jsonld'); $new_contents = $this->drupalGet($media_url . '?_format=jsonld');
$json = \GuzzleHttp\json_decode($new_contents, TRUE); $json = \GuzzleHttp\json_decode($new_contents, TRUE);
$this->assertEquals( $this->assertEquals(

4
tests/src/Functional/JsonldTypeAlterReactionTest.php

@ -75,6 +75,10 @@ class JsonldTypeAlterReactionTest extends JsonldSelfReferenceReactionTest {
$this->getSession()->getPage()->findById("edit-conditions-entity-bundle-context-mapping-node")->selectOption("@node.node_route_context:node"); $this->getSession()->getPage()->findById("edit-conditions-entity-bundle-context-mapping-node")->selectOption("@node.node_route_context:node");
$this->getSession()->getPage()->pressButton(t('Save and continue')); $this->getSession()->getPage()->pressButton(t('Save and continue'));
// The first time a Context is saved, you need to clear the cache.
// Subsequent changes to the context don't need a cache rebuild, though.
drupal_flush_all_caches();
// Check for the new @type from the field_type_predicate value. // Check for the new @type from the field_type_predicate value.
$new_contents = $this->drupalGet($url . '?_format=jsonld'); $new_contents = $this->drupalGet($url . '?_format=jsonld');
$json = \GuzzleHttp\json_decode($new_contents, TRUE); $json = \GuzzleHttp\json_decode($new_contents, TRUE);

Loading…
Cancel
Save