Created
July 17, 2015 17:23
-
-
Save kerasai/ddb24449fe090b7717f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Behat\Behat\Context\Context; | |
use Behat\Behat\Context\SnippetAcceptingContext; | |
/** | |
* Defines application features from the specific context. | |
*/ | |
class FeatureContext implements Context, SnippetAcceptingContext { | |
/** | |
* @Then :title field :field_name values should be from :vocab vocabulary | |
*/ | |
public function assertContentTermRefVocab($title, $field_name, $vocab) { | |
if (class_exists('\Drupal')) { | |
$version = \Drupal::VERSION; | |
} elseif (defined('VERSION')) { | |
$version = VERSION; | |
} | |
if (!isset($version)) { | |
throw new \Exception('Unable to determine Drupal version.'); | |
} | |
$major = explode('.', $version); | |
$major = reset($major); | |
switch ($major) { | |
case 7: | |
$this->assertContentTermRefVocab7($title, $field_name, $vocab); | |
break; | |
case 8: | |
$this->assertContentTermRefVocab8($title, $field_name, $vocab); | |
break; | |
default: | |
throw new \Exception('This version of Drupal not supported.'); | |
} | |
} | |
/** | |
* Check vocabulary on term reference field values, Drupal 8. | |
*/ | |
public function assertContentTermRefVocab8($title, $field_name, $vocab) { | |
$query = \Drupal::entityQuery('node')->condition('title', $title); | |
if (!$nids = $query->execute()) { | |
throw new \Exception(sprintf('No nodes with title %s.', $title)); | |
} | |
$nid = reset($nids); | |
$node = entity_load('node', $nid); | |
if (empty($node->{$field_name})) { | |
throw new \Exception(sprintf('No values set in field "%s".', $field_name)); | |
} | |
$values = $node->{$field_name}->referencedEntities(); | |
if (empty($values)) { | |
throw new \Exception(sprintf('No values set in field "%s".', $field_name)); | |
} | |
foreach ($values as $term) { | |
if ($term->getVocabularyId() != $vocab) { | |
throw new \Exception(sprintf('Term in field "%s" was from "%s" vocabulary, "%s" vocabulary expected.', $field_name, $term->getVocabularyId(), $vocab)); | |
} | |
} | |
} | |
/** | |
* Check vocabulary on term reference field values, Drupal 7. | |
*/ | |
public function assertContentTermRefVocab7($title, $field_name, $vocab) { | |
$query = new EntityFieldQuery(); | |
$query->entityCondition('entity_type', 'node'); | |
$result = $query->execute(); | |
if (empty($result['node'])) { | |
throw new \Exception(sprintf('No nodes with title %s.', $title)); | |
} | |
$nid = array_keys($result['node']); | |
$nid = reset($nid); | |
$node = node_load($nid); | |
$values = field_get_items('node', $node, $field_name); | |
if (empty($values)) { | |
throw new \Exception(sprintf('No values set in field "%s".', $field_name)); | |
} | |
foreach ($values as $item) { | |
$term = taxonomy_term_load($item['tid']); | |
if ($term->vocabulary_machine_name != $vocab) { | |
throw new \Exception(sprintf('Term in field "%s" was from "%s" vocabulary, "%s" vocabulary expected.', $field_name, $term->vocabulary_machine_name, $vocab)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment