Browse Source

Add tests for the `EntityBundle` and `JsonldAlterTypeReaction` plugins (Issue 992) (#110)

* EntityBundleTest WIP

* don't use route parameters to identify entity

* give taxonomy permissions and fix 'Hello World!' NOT in output

* WIP JsonldTypeAlterReactionTest

* working JsonldTypeAlterReactionTest

- remove buggy FieldUiTestTrait
- use schema:Organization for test predicate
- remove commented-out code
- add assertTrue for successful @type modification

* restore media type comment
pull/729/head
Seth Shaw 6 years ago committed by dannylamb
parent
commit
57070658ac
  1. 10
      src/Plugin/Condition/EntityBundle.php
  2. 43
      tests/src/Functional/EntityBundleTest.php
  3. 10
      tests/src/Functional/IslandoraFunctionalTestBase.php
  4. 87
      tests/src/Functional/JsonldTypeAlterReactionTest.php

10
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();
}

43
tests/src/Functional/EntityBundleTest.php

@ -0,0 +1,43 @@
<?php
namespace Drupal\Tests\islandora\Functional;
/**
* Tests the EntityBundle condition.
*
* @group islandora
*/
class EntityBundleTest extends IslandoraFunctionalTestBase {
/**
* @covers \Drupal\islandora\Plugin\Condition\EntityBundle::buildConfigurationForm
* @covers \Drupal\islandora\Plugin\Condition\EntityBundle::submitConfigurationForm
* @covers \Drupal\islandora\Plugin\Condition\EntityBundle::evaluate
*/
public function testEntityBundleType() {
// Create a test user.
$account = $this->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!");
}
}

10
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.
*/

87
tests/src/Functional/JsonldTypeAlterReactionTest.php

@ -0,0 +1,87 @@
<?php
namespace Drupal\Tests\islandora\Functional;
/**
* Class JsonldTypeAlterReactionTest.
*
* @package Drupal\Tests\islandora\Functional
* @group islandora
*/
class JsonldTypeAlterReactionTest extends MappingUriPredicateReactionTest {
/**
* @covers \Drupal\islandora\Plugin\ContextReaction\JsonldTypeAlterReaction
*/
public function testMappingReaction() {
$account = $this->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'
);
}
}
Loading…
Cancel
Save