diff --git a/src/Plugin/Condition/EntityBundle.php b/src/Plugin/Condition/EntityBundle.php index 5ec1a19c..96fa76d1 100644 --- a/src/Plugin/Condition/EntityBundle.php +++ b/src/Plugin/Condition/EntityBundle.php @@ -12,8 +12,8 @@ use Drupal\Core\Form\FormStateInterface; * id = "entity_bundle", * label = @Translation("Entity Bundle"), * context = { - * "node" = @ContextDefinition("entity:node", label = @Translation("Node")), - * "taxonomy_term" = @ContextDefinition("entity:taxonomy_term", label = @Translation("Term")) + * "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node")), + * "taxonomy_term" = @ContextDefinition("entity:taxonomy_term", required = FALSE, label = @Translation("Term")) * } * ) */ @@ -56,9 +56,9 @@ class EntityBundle extends ConditionPluginBase { * {@inheritdoc} */ public function evaluate() { - foreach (\Drupal::routeMatch()->getParameters()->keys() as $type) { - if ($this->getContext($type)->hasContextValue()) { - $entity = $this->getContextValue($type); + foreach ($this->getContexts() as $context) { + if ($context->hasContextValue()) { + $entity = $context->getContextValue(); if (!empty($this->configuration['bundles'][$entity->bundle()])) { return !$this->isNegated(); } diff --git a/tests/src/Functional/EntityBundleTest.php b/tests/src/Functional/EntityBundleTest.php new file mode 100644 index 00000000..1ffc261e --- /dev/null +++ b/tests/src/Functional/EntityBundleTest.php @@ -0,0 +1,43 @@ +drupalCreateUser([ + 'bypass node access', + 'administer contexts', + 'administer taxonomy', + ]); + $this->drupalLogin($account); + + $this->createContext('Test', 'test'); + $this->addCondition('test', 'entity_bundle'); + $this->getSession()->getPage()->checkField("edit-conditions-entity-bundle-bundles-test-type"); + $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->addPresetReaction('test', 'index', 'hello_world'); + + // Create a new test_type confirm Hello World! is printed to the screen. + $this->postNodeAddForm('test_type', ['title[0][value]' => 'Test Node'], 'Save'); + $this->assertSession()->pageTextContains("Hello World!"); + + // Create a new term and confirm Hellow World! is NOT printed to the screen. + $this->postTermAddForm('test_vocabulary', ['name[0][value]' => 'Test Term'], 'Save'); + $this->assertSession()->pageTextNotContains("Hello World!"); + + } + +} diff --git a/tests/src/Functional/IslandoraFunctionalTestBase.php b/tests/src/Functional/IslandoraFunctionalTestBase.php index 733d24a6..a689db78 100644 --- a/tests/src/Functional/IslandoraFunctionalTestBase.php +++ b/tests/src/Functional/IslandoraFunctionalTestBase.php @@ -20,7 +20,7 @@ class IslandoraFunctionalTestBase extends BrowserTestBase { use TestFileCreationTrait; use MediaFunctionalTestCreateMediaTypeTrait; - protected static $modules = ['context_ui', 'islandora']; + protected static $modules = ['context_ui', 'field_ui', 'islandora']; protected static $configSchemaCheckerExclusions = [ 'jwt.config', @@ -250,6 +250,14 @@ EOD; $this->assertSession()->statusCodeEquals(200); } + /** + * Create a new node by posting its add form. + */ + protected function postTermAddForm($taxomony_id, $values, $button_text) { + $this->drupalPostForm("admin/structure/taxonomy/manage/$taxomony_id/add", $values, t('@text', ['@text' => $button_text])); + $this->assertSession()->statusCodeEquals(200); + } + /** * Edits a node by posting its edit form. */ diff --git a/tests/src/Functional/JsonldTypeAlterReactionTest.php b/tests/src/Functional/JsonldTypeAlterReactionTest.php new file mode 100644 index 00000000..fdf12057 --- /dev/null +++ b/tests/src/Functional/JsonldTypeAlterReactionTest.php @@ -0,0 +1,87 @@ +drupalCreateUser([ + 'bypass node access', + 'administer contexts', + 'administer node fields', + ]); + $this->drupalLogin($account); + + // Add the typed predicate we will select in the reaction config. + // Taken from FieldUiTestTrait->fieldUIAddNewField. + $this->drupalPostForm('admin/structure/types/manage/test_type/fields/add-field', [ + 'new_storage_type' => 'string', + 'label' => 'Typed Predicate', + 'field_name' => 'type_predicate', + ], t('Save and continue')); + $this->drupalPostForm(NULL, [], t('Save field settings')); + $this->drupalPostForm(NULL, [], t('Save settings')); + $this->assertRaw('field_type_predicate', 'Redirected to "Manage fields" page.'); + + // Add the test node. + $this->postNodeAddForm('test_type', [ + 'title[0][value]' => 'Test Node', + 'field_type_predicate[0][value]' => 'schema:Organization', + ], t('Save')); + $this->assertSession()->pageTextContains("Test Node"); + $url = $this->getUrl(); + + // Make sure the node exists. + $this->drupalGet($url); + $this->assertSession()->statusCodeEquals(200); + + $contents = $this->drupalGet($url . '?_format=jsonld'); + $this->assertSession()->statusCodeEquals(200); + $json = \GuzzleHttp\json_decode($contents, TRUE); + $this->assertArrayHasKey('@type', + $json['@graph'][0], 'Missing @type'); + $this->assertEquals( + 'http://schema.org/Thing', + $json['@graph'][0]['@type'][0], + 'Missing @type value of http://schema.org/Thing' + ); + + // Add the test context. + $context_name = 'test'; + $reaction_id = 'alter_jsonld_type'; + + $this->createContext('Test', $context_name); + $this->drupalGet("admin/structure/context/$context_name/reaction/add/$reaction_id"); + $this->assertSession()->statusCodeEquals(200); + + $this->drupalGet("admin/structure/context/$context_name"); + $this->getSession()->getPage() + ->fillField("Source Field", "field_type_predicate"); + $this->getSession()->getPage()->pressButton("Save and continue"); + $this->assertSession() + ->pageTextContains("The context $context_name has been saved"); + + $this->addCondition('test', 'entity_bundle'); + $this->getSession()->getPage()->checkField("edit-conditions-entity-bundle-bundles-test-type"); + $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')); + + // Check for the new @type from the field_type_predicate value. + $new_contents = $this->drupalGet($url . '?_format=jsonld'); + $json = \GuzzleHttp\json_decode($new_contents, TRUE); + $this->assertTrue( + in_array('http://schema.org/Organization', $json['@graph'][0]['@type']), + 'Missing altered @type value of http://schema.org/Organization' + ); + } + +}