more module updates
This commit is contained in:
@@ -8,6 +8,9 @@ the module provides this additional functionality:
|
||||
* synonym-friendly autocomplete widget for taxonomy_term_reference fields
|
||||
* integration with Drupal search functionality enabling searching content by
|
||||
synonyms of the terms that the content references
|
||||
* integration with Search API. If you include synonyms of a term into your
|
||||
Search API search index, your clients will be able to find content with
|
||||
search keywords that contain synonyms and not actual names of terms.
|
||||
|
||||
-- REQUIREMENTS --
|
||||
|
||||
|
@@ -14,7 +14,9 @@ abstract class AbstractSynonymsExtractor {
|
||||
* Array of Field API field types from which this class is able to extract
|
||||
* synonyms
|
||||
*/
|
||||
static public abstract function fieldTypesSupported();
|
||||
public static function fieldTypesSupported() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract synonyms from a field attached to an entity.
|
||||
@@ -38,7 +40,9 @@ abstract class AbstractSynonymsExtractor {
|
||||
* @return array
|
||||
* Array of synonyms extracted from $items
|
||||
*/
|
||||
static public abstract function synonymsExtract($items, $field, $instance, $entity, $entity_type);
|
||||
public static function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow you to hook in during autocomplete suggestions generation.
|
||||
@@ -58,7 +62,7 @@ abstract class AbstractSynonymsExtractor {
|
||||
* @param array $instance
|
||||
* Array of instance definition according to Field API
|
||||
*/
|
||||
static public abstract function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance);
|
||||
public static function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {}
|
||||
|
||||
/**
|
||||
* Add an entity as a synonym into a field of another entity.
|
||||
@@ -91,22 +95,22 @@ abstract class AbstractSynonymsExtractor {
|
||||
* Array of extra items to be merged into the items that already exist in
|
||||
* field values
|
||||
*/
|
||||
static public abstract function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type);
|
||||
public static function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Supportive method.
|
||||
*
|
||||
* Set such a condition on $query that it will always yield no results. Should
|
||||
* be called from $this->processEntityFieldQuery() when for whatever reason
|
||||
* the object can't alter $query to include matched synonyms. As a fallback
|
||||
* it should call this method to make sure it filtered everything out.
|
||||
* be called from $this->processEntityFieldQuery() when for whatever reason
|
||||
* the object can't alter $query to include matched synonyms. As a fallback it
|
||||
* should call this method to make sure it filtered everything out.
|
||||
*
|
||||
* @param EntityFieldQuery $query
|
||||
* Query object passed to $this->processEntityFieldQuery() method
|
||||
* @param array $field
|
||||
* Field array passed to $this->processEntityFieldQuery() method
|
||||
*/
|
||||
static protected function emptyResultsCondition(EntityFieldQuery $query) {
|
||||
$query->entityCondition('entity_id', -1);
|
||||
protected static function emptyResultsCondition(EntityFieldQuery $query) {
|
||||
$query->range(0, 0);
|
||||
}
|
||||
}
|
||||
|
@@ -7,11 +7,11 @@
|
||||
|
||||
class EntityReferenceSynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
|
||||
static public function fieldTypesSupported() {
|
||||
public static function fieldTypesSupported() {
|
||||
return array('entityreference');
|
||||
}
|
||||
|
||||
static public function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
public static function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
$synonyms = array();
|
||||
|
||||
// For speading up loading all the entities at once.
|
||||
@@ -27,7 +27,7 @@ class EntityReferenceSynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
static public function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
|
||||
public static function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
|
||||
// Unfortunately EntityFieldQuery does not currently support INNER JOINing
|
||||
// referenced entities via any field type.
|
||||
// Thus, we use an ugly solution -- going through all entities that exist
|
||||
@@ -55,9 +55,9 @@ class EntityReferenceSynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
$query->fieldCondition($field, 'target_id', array_keys($result));
|
||||
}
|
||||
|
||||
static public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
// Firstly validating that this entity refernce is able to reference to that
|
||||
// type of entity.
|
||||
public static function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
// Firstly validating that this entity reference is able to reference to
|
||||
// that type of entity.
|
||||
$expected_synonym_entity_type = $field['settings']['target_type'];
|
||||
if ($expected_synonym_entity_type != $synonym_entity_type) {
|
||||
return array();
|
||||
@@ -67,5 +67,4 @@ class EntityReferenceSynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
'target_id' => $synonym_entity_id,
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -7,11 +7,11 @@
|
||||
|
||||
class SynonymsSynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
|
||||
static public function fieldTypesSupported() {
|
||||
public static function fieldTypesSupported() {
|
||||
return array('text', 'number_integer', 'number_float', 'number_decimal');
|
||||
}
|
||||
|
||||
static public function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
public static function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
$synonyms = array();
|
||||
|
||||
foreach ($items as $item) {
|
||||
@@ -21,11 +21,11 @@ class SynonymsSynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
static public function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
|
||||
public static function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
|
||||
$query->fieldCondition($field, 'value', '%' . $tag . '%', 'LIKE');
|
||||
}
|
||||
|
||||
static public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
public static function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
$synonym = entity_label($synonym_entity_type, $synonym_entity);
|
||||
switch ($field['type']) {
|
||||
case 'text':
|
||||
|
@@ -7,11 +7,11 @@
|
||||
|
||||
class TaxonomySynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
|
||||
static public function fieldTypesSupported() {
|
||||
public static function fieldTypesSupported() {
|
||||
return array('taxonomy_term_reference');
|
||||
}
|
||||
|
||||
static public function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
public static function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
$synonyms = array();
|
||||
|
||||
$terms = array();
|
||||
@@ -25,7 +25,7 @@ class TaxonomySynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
static public function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
|
||||
public static function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
|
||||
// Unfortunately EntityFieldQuery does not currently support INNER JOINing
|
||||
// term entity that is referenced via taxonomy_term_reference field type.
|
||||
// Thus, we use an ugly solution -- going through all terms that exist in
|
||||
@@ -56,7 +56,7 @@ class TaxonomySynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
$query->fieldCondition($field, 'tid', $tids);
|
||||
}
|
||||
|
||||
static public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
public static function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
// Taxonomy term reference supports only referencing of entity types
|
||||
// 'taxonomy_term'.. duh.
|
||||
if ($synonym_entity_type != 'taxonomy_term') {
|
||||
|
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
function hook_synonyms_extractor_info() {
|
||||
return array(
|
||||
// Please see below the defintion of ApiSynonymsSynonymsExtractor class
|
||||
// Please see below the definition of ApiSynonymsSynonymsExtractor class
|
||||
// for your reference.
|
||||
'ApiSynonymsSynonymsExtractor',
|
||||
);
|
||||
@@ -28,7 +28,7 @@ function hook_synonyms_extractor_info() {
|
||||
*
|
||||
* This is a copy of SynonymsSynonymsExtractor class providing an example of
|
||||
* how to write your own synonyms extractor class. See the definition of
|
||||
* AbstractSynonymsExtractor for reference and incode comments. For more
|
||||
* AbstractSynonymsExtractor for reference and in code comments. For more
|
||||
* complicated examples take a look at EntityReferenceSynonymsExtractor class.
|
||||
*/
|
||||
class ApiSynonymsSynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
|
@@ -10,10 +10,12 @@ files[] = includes/AbstractSynonymsExtractor.class.inc
|
||||
files[] = includes/SynonymsSynonymsExtractor.class.inc
|
||||
files[] = includes/TaxonomySynonymsExtractor.class.inc
|
||||
files[] = includes/EntityReferenceSynonymsExtractor.class.inc
|
||||
files[] = views/synonyms.views.inc
|
||||
files[] = views/synonyms_views_plugin_argument_validate_taxonomy_term.inc
|
||||
|
||||
; Information added by drupal.org packaging script on 2013-01-15
|
||||
version = "7.x-1.1"
|
||||
; Information added by Drupal.org packaging script on 2014-09-15
|
||||
version = "7.x-1.2"
|
||||
core = "7.x"
|
||||
project = "synonyms"
|
||||
datestamp = "1358220389"
|
||||
datestamp = "1410753528"
|
||||
|
||||
|
@@ -16,16 +16,95 @@ define('SYNONYMS_DEFAULT_FIELD_NAME', 'synonyms_synonyms');
|
||||
function synonyms_menu() {
|
||||
$items = array();
|
||||
|
||||
$items['synonyms/autocomplete'] = array(
|
||||
$items['synonyms/autocomplete/%/%/%'] = array(
|
||||
'title' => 'Autocomplete Synonyms',
|
||||
'page callback' => 'synonyms_autocomplete',
|
||||
'page arguments' => array(2, 3, 4),
|
||||
'access arguments' => array('access content'),
|
||||
'file' => 'synonyms.pages.inc',
|
||||
'type' => MENU_CALLBACK,
|
||||
);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_taxonomy_term_update().
|
||||
*/
|
||||
function synonyms_taxonomy_term_update($term) {
|
||||
// If we notice at least some change in synonyms of this term, we want to
|
||||
// trigger search re-indexing of nodes, where this term is referenced, since
|
||||
// change in term synonyms affects nodes ranking in the search.
|
||||
if (isset($term->original)) {
|
||||
$current_synonyms = synonyms_get_raw($term);
|
||||
$previous_synonyms = synonyms_get_raw($term->original);
|
||||
if (count($current_synonyms) != count($previous_synonyms)) {
|
||||
// Schedule re-indexing, because amount of synonyms has changed.
|
||||
module_load_include('inc', 'synonyms', 'synonyms.pages');
|
||||
synonyms_reindex_nodes_by_terms(array($term->tid));
|
||||
}
|
||||
else {
|
||||
foreach ($current_synonyms as $k => $current_synonym) {
|
||||
if ($current_synonyms[$k] != $previous_synonyms[$k]) {
|
||||
// Schedule re-indexing, because some synonym has changed.
|
||||
module_load_include('inc', 'synonyms', 'synonyms.pages');
|
||||
synonyms_reindex_nodes_by_terms(array($term->tid));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_property_info().
|
||||
*/
|
||||
function synonyms_entity_property_info() {
|
||||
$info = array();
|
||||
$properties = &$info['taxonomy_term']['properties'];
|
||||
$properties['synonyms'] = array(
|
||||
'label' => t('Synonyms'),
|
||||
'description' => t('Synonyms of entity.'),
|
||||
'type' => 'list<text>',
|
||||
'getter callback' => 'synonyms_get_sanitized',
|
||||
'computed' => TRUE,
|
||||
'sanitized' => TRUE,
|
||||
'raw getter callback' => 'synonyms_get_raw',
|
||||
);
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_node_update_index().
|
||||
*/
|
||||
function synonyms_node_update_index($node) {
|
||||
$output = '';
|
||||
foreach (field_info_instances('node', $node->type) as $instance) {
|
||||
// We go a field by field looking for taxonomy term reference and
|
||||
// if that vocabulary has enabled synonyms, we add term's synonyms
|
||||
// to the search index.
|
||||
$field_info = field_info_field($instance['field_name']);
|
||||
if ($field_info['type'] == 'taxonomy_term_reference') {
|
||||
// For each term referenced in this node we have to add synonyms.
|
||||
$_terms = field_get_items('node', $node, $instance['field_name']);
|
||||
if (is_array($_terms) && !empty($_terms)) {
|
||||
$terms = array();
|
||||
foreach ($_terms as $v) {
|
||||
$terms[] = $v['tid'];
|
||||
}
|
||||
$terms = taxonomy_term_load_multiple($terms);
|
||||
foreach ($terms as $term) {
|
||||
$synonyms = synonyms_get_sanitized($term);
|
||||
if (!empty($synonyms)) {
|
||||
$output .= '<strong>' . implode(', ', $synonyms) . '</strong>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_synonyms_extractor_info().
|
||||
*/
|
||||
@@ -53,7 +132,7 @@ function synonyms_synonyms_extractor_info() {
|
||||
* Whether collect all the info again from hooks, or cached info is fine
|
||||
*
|
||||
* @return array
|
||||
* Key of this array denote the field type while value of the array denotes
|
||||
* Key of this array denotes the field type while value of the array denotes
|
||||
* which class reports ability to extract synonyms from such field type.
|
||||
*/
|
||||
function synonyms_extractor_info($field_type = NULL, $reset = FALSE) {
|
||||
@@ -72,10 +151,8 @@ function synonyms_extractor_info($field_type = NULL, $reset = FALSE) {
|
||||
|
||||
if (is_array($extractor_classes)) {
|
||||
foreach ($extractor_classes as $class) {
|
||||
if (class_exists($class) && is_subclass_of($class, 'AbstractSynonymsExtractor')) {
|
||||
foreach ($class::fieldTypesSupported() as $_field_type) {
|
||||
$info[$_field_type] = $class;
|
||||
}
|
||||
foreach (call_user_func(array($class, 'fieldTypesSupported')) as $_field_type) {
|
||||
$info[$_field_type] = $class;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,13 +161,6 @@ function synonyms_extractor_info($field_type = NULL, $reset = FALSE) {
|
||||
// data.
|
||||
drupal_alter('synonyms_extractor_info', $info);
|
||||
|
||||
// Validating that any changes made to $info do not break class hierarchy.
|
||||
foreach ($info as $k => $v) {
|
||||
if (!class_exists($v) || !is_subclass_of($v, 'AbstractSynonymsExtractor')) {
|
||||
unset($info[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
$cache = $info;
|
||||
cache_set($cid, $cache, 'cache', CACHE_TEMPORARY);
|
||||
}
|
||||
@@ -106,41 +176,6 @@ function synonyms_extractor_info($field_type = NULL, $reset = FALSE) {
|
||||
return $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_node_update_index().
|
||||
*/
|
||||
function synonyms_node_update_index($node) {
|
||||
$output = '';
|
||||
foreach (field_info_instances('node', $node->type) as $instance) {
|
||||
// We go a field by field looking for taxonomy term reference and
|
||||
// if that vocabulary has enabled synonyms, we add term's synonyms
|
||||
// to the search index.
|
||||
$field_info = field_info_field($instance['field_name']);
|
||||
if ($field_info['type'] == 'taxonomy_term_reference') {
|
||||
// For each term referenced in this node we have to add synonyms.
|
||||
$_terms = field_get_items('node', $node, $instance['field_name']);
|
||||
if (is_array($_terms) && !empty($_terms)) {
|
||||
$terms = array();
|
||||
foreach ($_terms as $v) {
|
||||
$terms[] = $v['tid'];
|
||||
}
|
||||
$terms = taxonomy_term_load_multiple($terms);
|
||||
foreach ($terms as $term) {
|
||||
$synonyms = synonyms_get_term_synonyms($term);
|
||||
if (!empty($synonyms)) {
|
||||
$safe_synonyms = array();
|
||||
foreach ($synonyms as $synonym) {
|
||||
$safe_synonyms[] = $synonym['safe_value'];
|
||||
}
|
||||
$output .= '<strong>' . implode(', ', $safe_synonyms) . '</strong>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_FORM_ID_alter().
|
||||
*/
|
||||
@@ -149,6 +184,8 @@ function synonyms_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
|
||||
return;
|
||||
}
|
||||
|
||||
module_load_include('inc', 'synonyms', 'synonyms.pages');
|
||||
|
||||
$form['synonyms'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('Synonyms'),
|
||||
@@ -160,7 +197,7 @@ function synonyms_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
|
||||
SYNONYMS_DEFAULT_FIELD_NAME => t('Default synonyms field'),
|
||||
);
|
||||
if (isset($form['#vocabulary']->vid)) {
|
||||
$instances = synonyms_instances_extract_applicapable($form['#vocabulary']);
|
||||
$instances = synonyms_instances_extract_applicable($form['#vocabulary']);
|
||||
foreach ($instances as $instance) {
|
||||
// Here we prefer some informative text for the default synonyms field
|
||||
// rather its label.
|
||||
@@ -178,7 +215,7 @@ function synonyms_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
|
||||
'#default_value' => synonyms_synonyms_fields($form['#vocabulary']),
|
||||
);
|
||||
|
||||
// Adding out own submit handler.
|
||||
// Adding our own submit handler.
|
||||
$form['#submit'][] = 'synonyms_taxonomy_form_vocabulary_submit';
|
||||
}
|
||||
|
||||
@@ -193,6 +230,9 @@ function synonyms_field_widget_info() {
|
||||
'settings' => array(
|
||||
'size' => 60,
|
||||
'autocomplete_path' => 'synonyms/autocomplete',
|
||||
'suggestion_size' => 10,
|
||||
'suggest_only_unique' => FALSE,
|
||||
'auto_creation' => 1,
|
||||
),
|
||||
'behaviors' => array(
|
||||
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
|
||||
@@ -214,7 +254,23 @@ function synonyms_field_widget_settings_form($field, $instance) {
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Allow auto-creation?'),
|
||||
'#description' => t('Whether users may create a new term by typing in a non-existing name into this field.'),
|
||||
'#default_value' => isset($settings['auto_creation']) ? $settings['auto_creation'] : 0,
|
||||
'#default_value' => $settings['auto_creation'],
|
||||
);
|
||||
|
||||
$form['suggestion_size'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Suggestions Size'),
|
||||
'#description' => t('Please, enter how many suggested entities to show in the autocomplete textfield.'),
|
||||
'#required' => TRUE,
|
||||
'#element_validate' => array('element_validate_integer_positive'),
|
||||
'#default_value' => $settings['suggestion_size'],
|
||||
);
|
||||
|
||||
$form['suggest_only_unique'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Suggest only one entry per term'),
|
||||
'#description' => t('If you want to include only term name or a single synonym, suggesting a particular term, while disregarding all ongoing ones, please, tick this checkbox on.'),
|
||||
'#default_value' => $settings['suggest_only_unique'],
|
||||
);
|
||||
|
||||
return $form;
|
||||
@@ -232,11 +288,11 @@ function synonyms_field_widget_form(&$form, &$form_state, $field, $instance, $la
|
||||
$element += array(
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => taxonomy_implode_tags($tags),
|
||||
'#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'] . '/' . $field['field_name'],
|
||||
'#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'] . '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'],
|
||||
'#size' => $instance['widget']['settings']['size'],
|
||||
'#maxlength' => 1024,
|
||||
'#element_validate' => array('taxonomy_autocomplete_validate', 'synonyms_autocomplete_validate'),
|
||||
'#auto_creation' => isset($instance['widget']['settings']['auto_creation']) ? $instance['widget']['settings']['auto_creation'] : 0,
|
||||
'#auto_creation' => $instance['widget']['settings']['auto_creation'],
|
||||
);
|
||||
|
||||
return $element;
|
||||
@@ -255,226 +311,43 @@ function synonyms_field_widget_error($element, $error, $form, &$form_state) {
|
||||
* Handle validation for taxonomy term synonym-friendly autocomplete element.
|
||||
*/
|
||||
function synonyms_autocomplete_validate($element, &$form_state) {
|
||||
// After taxonomy_autocomplete_validate() has finished its job
|
||||
// it might left terms in the format for autocreation. Since our field
|
||||
// supports auto creation as a configurable option, we have to make sure
|
||||
// auto creation terms are allowed.
|
||||
// After taxonomy_autocomplete_validate() has finished its job any terms it
|
||||
// didn't find have been set for autocreation. We need to:
|
||||
// (a) Double-check that those terms are not synonyms.
|
||||
// (b) Check that synonyms' configurable auto-creation option is enabled.
|
||||
$value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
|
||||
if (!$element['#auto_creation']) {
|
||||
// Deleting all the terms meant to be auto-created.
|
||||
foreach ($value as $delta => $term) {
|
||||
if ($term['tid'] == 'autocreate') {
|
||||
foreach ($value as $delta => $term) {
|
||||
if ($term['tid'] == 'autocreate') {
|
||||
$vocabulary = taxonomy_vocabulary_load($term['vid']);
|
||||
$synonym_tid = synonyms_get_term_by_synonym($term['name'], $vocabulary);
|
||||
if ($synonym_tid != 0) {
|
||||
$value[$delta]['tid'] = $synonym_tid;
|
||||
}
|
||||
elseif (empty($element['#auto_creation'])) {
|
||||
unset($value[$delta]);
|
||||
}
|
||||
}
|
||||
$value = array_values($value);
|
||||
}
|
||||
|
||||
$value = array_values($value);
|
||||
form_set_value($element, $value, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit hanlder for Taxonomy vocabulary edit form.
|
||||
*
|
||||
* Store extra values attached to form in this module.
|
||||
*/
|
||||
function synonyms_taxonomy_form_vocabulary_submit($form, &$form_state) {
|
||||
$values = $form_state['values'];
|
||||
|
||||
if ($values['op'] == $form['actions']['submit']['#value']) {
|
||||
if (isset($form['#vocabulary']->vid)) {
|
||||
$vocabulary = taxonomy_vocabulary_load($form['#vocabulary']->vid);
|
||||
}
|
||||
else {
|
||||
// As a fallback, if this is a just created vocabulary, we try to pull it
|
||||
// up by the just submitted machine name.
|
||||
$vocabulary = taxonomy_vocabulary_machine_name_load($values['machine_name']);
|
||||
}
|
||||
|
||||
// Preprocessing values keeping only field names of the checked fields.
|
||||
foreach ($values['synonyms']['synonyms'] as $k => $v) {
|
||||
if (!$v) {
|
||||
unset($values['synonyms']['synonyms'][$k]);
|
||||
}
|
||||
}
|
||||
$values['synonyms']['synonyms'] = array_values($values['synonyms']['synonyms']);
|
||||
$settings = synonyms_vocabulary_settings($vocabulary);
|
||||
$settings = $values['synonyms'] + $settings;
|
||||
synonyms_vocabulary_settings_save($vocabulary, $settings);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Page callback: Outputs JSON for taxonomy autocomplete suggestions.
|
||||
*
|
||||
* This callback outputs term name suggestions in response to Ajax requests
|
||||
* made by the synonyms autocomplete widget for taxonomy term reference
|
||||
* fields. The output is a JSON object of plain-text term suggestions,
|
||||
* keyed by the user-entered value with the completed term name appended.
|
||||
* Term names containing commas are wrapped in quotes. The search is made
|
||||
* with consideration of synonyms.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The name of the term reference field.
|
||||
* @param string $tags_typed
|
||||
* (optional) A comma-separated list of term names entered in the
|
||||
* autocomplete form element. Only the last term is used for autocompletion.
|
||||
* Defaults to '' (an empty string).
|
||||
*/
|
||||
function synonyms_autocomplete($field_name, $tags_typed = '') {
|
||||
// How many suggestions maximum we are able to output.
|
||||
$max_suggestions = 10;
|
||||
|
||||
// If the request has a '/' in the search text, then the menu system will have
|
||||
// split it into multiple arguments, recover the intended $tags_typed.
|
||||
$args = func_get_args();
|
||||
// Shift off the $field_name argument.
|
||||
array_shift($args);
|
||||
$tags_typed = implode('/', $args);
|
||||
|
||||
// Make sure the field exists and is a taxonomy field.
|
||||
if (!($field = field_info_field($field_name)) || $field['type'] != 'taxonomy_term_reference') {
|
||||
// Error string. The JavaScript handler will realize this is not JSON and
|
||||
// will display it as debugging information.
|
||||
print t('Taxonomy field @field_name not found.', array('@field_name' => $field_name));
|
||||
exit;
|
||||
}
|
||||
|
||||
// The user enters a comma-separated list of tags. We only autocomplete the
|
||||
// last tag.
|
||||
$tags_typed = drupal_explode_tags($tags_typed);
|
||||
$tag_last = drupal_strtolower(array_pop($tags_typed));
|
||||
|
||||
$term_matches = array();
|
||||
if ($tag_last != '') {
|
||||
// Part of the criteria for the query come from the field's own settings.
|
||||
$vocabularies = array();
|
||||
$tmp = taxonomy_vocabulary_get_names();
|
||||
foreach ($field['settings']['allowed_values'] as $tree) {
|
||||
$vocabularies[$tmp[$tree['vocabulary']]->vid] = $tree['vocabulary'];
|
||||
}
|
||||
$vocabularies = taxonomy_vocabulary_load_multiple(array_keys($vocabularies));
|
||||
|
||||
// Firstly getting a list of tids that match by $term->name.
|
||||
$query = db_select('taxonomy_term_data', 't');
|
||||
$query->addTag('translatable');
|
||||
$query->addTag('term_access');
|
||||
|
||||
// Do not select already entered terms.
|
||||
if (!empty($tags_typed)) {
|
||||
$query->condition('t.name', $tags_typed, 'NOT IN');
|
||||
}
|
||||
// Select rows that match by term name.
|
||||
$tags_return = $query
|
||||
->fields('t', array('tid', 'name'))
|
||||
->condition('t.vid', array_keys($vocabularies))
|
||||
->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
|
||||
->range(0, $max_suggestions)
|
||||
->execute()
|
||||
->fetchAllKeyed();
|
||||
|
||||
// Converting results into another format.
|
||||
foreach ($tags_return as $tid => $name) {
|
||||
$tags_return[$tid] = array('name' => $name);
|
||||
}
|
||||
|
||||
$synonym_tids = array();
|
||||
// Now we go vocabulary by vocabulary looking through synonym fields.
|
||||
foreach ($vocabularies as $vocabulary) {
|
||||
// Now we go a synonym field by synonym field gathering suggestions.
|
||||
// Since officially EntityFieldQuery doesn't support OR conditions
|
||||
// we are enforced to go a field by field querying multiple times the DB.
|
||||
$bundle = field_extract_bundle('taxonomy_term', $vocabulary);
|
||||
foreach (synonyms_synonyms_fields($vocabulary) as $field) {
|
||||
$field = field_info_field($field);
|
||||
$instance = field_info_instance('taxonomy_term', $field['field_name'], $bundle);
|
||||
if (count($tags_return) + count($synonym_tids) > $max_suggestions) {
|
||||
// We have collected enough suggestions and the ongoing queries
|
||||
// will be just a waste of time.
|
||||
break;
|
||||
}
|
||||
$query = new EntityFieldQuery();
|
||||
$query->entityCondition('entity_type', 'taxonomy_term')
|
||||
->entityCondition('bundle', $vocabulary->machine_name);
|
||||
|
||||
// We let the class that defines this field type as a source of synonyms
|
||||
// filter our and provide its suggestions based on this field.
|
||||
$class = synonyms_extractor_info($field['type']);
|
||||
$class::processEntityFieldQuery($tag_last, $query, $field, $instance);
|
||||
|
||||
if (!empty($tags_typed)) {
|
||||
$query->propertyCondition('name', $tags_typed, 'NOT IN');
|
||||
}
|
||||
if (!empty($tags_return)) {
|
||||
// We don't want to search among the terms already found by term name.
|
||||
$query->entityCondition('entity_id', array_keys($tags_return), 'NOT IN');
|
||||
}
|
||||
if (!empty($synonym_tids)) {
|
||||
// Nor we want to search among the terms already mached by previous
|
||||
// synonym fields.
|
||||
$query->entityCondition('entity_id', $synonym_tids, 'NOT IN');
|
||||
}
|
||||
$tmp = $query->execute();
|
||||
if (!empty($tmp)) {
|
||||
// Merging the results.
|
||||
$tmp = array_keys($tmp['taxonomy_term']);
|
||||
$synonym_tids = array_merge($synonym_tids, $tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($synonym_tids)) {
|
||||
$synonym_tids = array_slice($synonym_tids, 0, $max_suggestions - count($tags_return));
|
||||
foreach (taxonomy_term_load_multiple($synonym_tids) as $synonym_term) {
|
||||
$tags_return[$synonym_term->tid] = array('name' => $synonym_term->name);
|
||||
// Additionally we have to find out which synonym triggered inclusion
|
||||
// of this term.
|
||||
$synonyms = synonyms_get_term_synonyms($synonym_term);
|
||||
foreach ($synonyms as $item) {
|
||||
if (strpos(mb_strtoupper($item['value'], 'UTF-8'), mb_strtoupper($tag_last, 'UTF-8')) !== FALSE) {
|
||||
$tags_return[$synonym_term->tid]['synonym'] = $item['value'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
|
||||
|
||||
// Now formatting the results.
|
||||
foreach ($tags_return as $tid => $info) {
|
||||
$n = $info['name'];
|
||||
// Term names containing commas or quotes must be wrapped in quotes.
|
||||
if (strpos($info['name'], ',') !== FALSE || strpos($info['name'], '"') !== FALSE) {
|
||||
$n = '"' . str_replace('"', '""', $info['name']) . '"';
|
||||
}
|
||||
|
||||
if (isset($info['synonym'])) {
|
||||
$display_name = t('@synonym, synonym of %term', array('@synonym' => $info['synonym'], '%term' => $info['name']));
|
||||
}
|
||||
else {
|
||||
$display_name = check_plain($info['name']);
|
||||
}
|
||||
$term_matches[$prefix . $n] = $display_name;
|
||||
}
|
||||
}
|
||||
drupal_json_output($term_matches);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to find a term by its name or synonym.
|
||||
*
|
||||
* To maximize the match trimming and case-insensetive comparison is used.
|
||||
* To maximize the match trimming and case-insensitive comparison is used.
|
||||
*
|
||||
* @param string $name
|
||||
* The string to be searched for its {taxonomy_term}.tid
|
||||
* The string to be searched for its {taxonomy_term_data}.tid
|
||||
* @param object $vocabulary
|
||||
* Fully loaded vocabulary object in which you wish to search
|
||||
* @param int $parent
|
||||
* Optional. In case you want to narrow your search scope, this parameter
|
||||
* takes in the {taxonomy_term}.tid of the parent term, letting you search
|
||||
* only among its children
|
||||
* takes in the {taxonomy_term_data}.tid of the parent term, letting you
|
||||
* search only among its children
|
||||
*
|
||||
* @return int
|
||||
* If the look up was successfull returns the {taxonomy_term}.tid of the
|
||||
* If the look up was successful returns the {taxonomy_term_data}.tid of the
|
||||
* found term, otherwise returns 0
|
||||
*/
|
||||
function synonyms_get_term_by_synonym($name, $vocabulary, $parent = 0) {
|
||||
@@ -487,9 +360,9 @@ function synonyms_get_term_by_synonym($name, $vocabulary, $parent = 0) {
|
||||
}
|
||||
|
||||
// We additionally scan through the synonyms.
|
||||
$synonyms = synonyms_get_term_synonyms($term);
|
||||
$synonyms = synonyms_get_sanitized($term);
|
||||
foreach ($synonyms as $item) {
|
||||
if (mb_strtoupper($item['safe_value'], 'UTF-8') == $name) {
|
||||
if (mb_strtoupper($item, 'UTF-8') == $name) {
|
||||
return $term->tid;
|
||||
}
|
||||
}
|
||||
@@ -503,21 +376,21 @@ function synonyms_get_term_by_synonym($name, $vocabulary, $parent = 0) {
|
||||
/**
|
||||
* Look up a term considering synonyms and if nothing found add one.
|
||||
*
|
||||
* This function is useful for automated creation of new terms
|
||||
* as it won't generate the same terms over and over again.
|
||||
* This function is useful for automated creation of new terms as it won't
|
||||
* generate the same terms over and over again.
|
||||
*
|
||||
* @param string $name
|
||||
* The string to be searched for its {taxonomy_term}.tid
|
||||
* The string to be searched for its {taxonomy_term_data}.tid
|
||||
* @param object $vocabulary
|
||||
* Fully loaded vocabulary object in which you wish to search
|
||||
* @param int $parent
|
||||
* Optional. In case you want to narrow your search scope, this parameter
|
||||
* takes in the {taxonomy_term}.tid of the parent term, letting you search
|
||||
* only among its children
|
||||
* takes in the {taxonomy_term_data}.tid of the parent term, letting you
|
||||
* search only among its children
|
||||
*
|
||||
* @return int
|
||||
* If a term already exists, its {taxonomy_term}.tid is returned,
|
||||
* otherwise it creates a new term and returns its {taxonomy_term}.tid
|
||||
* If a term already exists, its {taxonomy_term_data}.tid is returned,
|
||||
* otherwise it creates a new term and returns its {taxonomy_term_data}.tid
|
||||
*/
|
||||
function synonyms_add_term_by_synonym($name, $vocabulary, $parent = 0) {
|
||||
$tid = synonyms_get_term_by_synonym($name, $vocabulary, $parent);
|
||||
@@ -544,6 +417,40 @@ function synonyms_add_term_by_synonym($name, $vocabulary, $parent = 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of sanitized synonyms of a taxonomy term.
|
||||
*
|
||||
* @param $item object
|
||||
* Fully loaded taxonomy term
|
||||
*
|
||||
* @return array
|
||||
* List of sanitized synonyms of a taxonomy term
|
||||
*/
|
||||
function synonyms_get_sanitized($item) {
|
||||
$synonyms = array();
|
||||
foreach (synonyms_get_term_synonyms($item) as $synonym) {
|
||||
$synonyms[] = $synonym['safe_value'];
|
||||
}
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of raw synonyms of a taxonomy term.
|
||||
*
|
||||
* @param $item object
|
||||
* Fully loaded taxonomy term
|
||||
*
|
||||
* @return array
|
||||
* List of raw synonyms of a taxonomy term
|
||||
*/
|
||||
function synonyms_get_raw($item) {
|
||||
$synonyms = array();
|
||||
foreach (synonyms_get_term_synonyms($item) as $synonym) {
|
||||
$synonyms[] = $synonym['value'];
|
||||
}
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public function for retrieving synonyms of a taxonomy term.
|
||||
*
|
||||
@@ -552,10 +459,10 @@ function synonyms_add_term_by_synonym($name, $vocabulary, $parent = 0) {
|
||||
*
|
||||
* @return array
|
||||
* Array of synonyms, if synonyms are disabled for the taxonomy term's
|
||||
* vocabulary, an empty array is returned. Each synonyms array consists of the
|
||||
* following keys:
|
||||
* value - the value of a synonym as it was input by user
|
||||
* safe_value - a sanitized value of a synonym
|
||||
* vocabulary, an empty array is returned. Each synonym subarray consists of
|
||||
* the following keys:
|
||||
* - value: (string) the value of a synonym as it was input by user
|
||||
* - safe_value: (string) a sanitized value of a synonym
|
||||
*/
|
||||
function synonyms_get_term_synonyms($term) {
|
||||
$synonyms = array();
|
||||
@@ -568,18 +475,15 @@ function synonyms_get_term_synonyms($term) {
|
||||
|
||||
if (is_array($items) && !empty($items)) {
|
||||
$class = synonyms_extractor_info($field['type']);
|
||||
$synonyms = array_merge($synonyms, $class::synonymsExtract($items, $field, $instance, $term, 'taxonomy_term'));
|
||||
foreach (call_user_func(array($class, 'synonymsExtract'), $items, $field, $instance, $term, 'taxonomy_term') as $synonym) {
|
||||
$synonyms[] = array(
|
||||
'value' => $synonym,
|
||||
'safe_value' => check_plain($synonym),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Applying sanitization to the extracted synonyms.
|
||||
foreach ($synonyms as $k => $v) {
|
||||
$synonyms[$k] = array(
|
||||
'value' => $v,
|
||||
'safe_value' => check_plain($v),
|
||||
);
|
||||
}
|
||||
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
@@ -626,7 +530,7 @@ function synonyms_add_entity_as_synonym($trunk_entity, $trunk_entity_type, $fiel
|
||||
$trunk_entity_ids = entity_extract_ids($trunk_entity_type, $trunk_entity);
|
||||
$instance = field_info_instance($trunk_entity_type, $field['field_name'], $trunk_entity_ids[2]);
|
||||
|
||||
$extra_items = $extractor::mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type);
|
||||
$extra_items = call_user_func(array($extractor, 'mergeEntityAsSynonym'), $items, $field, $instance, $synonym_entity, $synonym_entity_type);
|
||||
|
||||
// Merging extracted synonym items into the values of the field that already
|
||||
// exist.
|
||||
@@ -655,7 +559,7 @@ function synonyms_add_entity_as_synonym($trunk_entity, $trunk_entity_type, $fiel
|
||||
function synonyms_synonyms_fields($vocabulary) {
|
||||
$settings = synonyms_vocabulary_settings($vocabulary);
|
||||
if (!isset($settings['synonyms']) || !is_array($settings['synonyms'])) {
|
||||
// Wierd as normally we expect to see here at least an empty array but
|
||||
// Weird as normally we expect to see here at least an empty array but
|
||||
// no problem. We simply initialize it.
|
||||
$settings['synonyms'] = array();
|
||||
}
|
||||
@@ -717,13 +621,16 @@ function synonyms_synonyms_enforce($vocabulary, $fields) {
|
||||
* @return array
|
||||
* Array of current synonyms settings for the supplied vocabulary.
|
||||
* Should include the following keys:
|
||||
* synonyms - array of field names that are enabled as source of synonyms
|
||||
* - synonyms: (array) array of field names that are enabled as source of
|
||||
* synonyms
|
||||
*/
|
||||
function synonyms_vocabulary_settings($vocabulary) {
|
||||
$settings = array();
|
||||
|
||||
if (isset($vocabulary->vid)) {
|
||||
$settings = variable_get('synonyms_settings_' . $vocabulary->vid, array());
|
||||
$settings = variable_get('synonyms_settings_' . $vocabulary->vid, array(
|
||||
'synonyms' => array(),
|
||||
));
|
||||
}
|
||||
|
||||
return $settings;
|
||||
@@ -740,6 +647,15 @@ function synonyms_vocabulary_settings($vocabulary) {
|
||||
* synonyms_vocabulary_settings()
|
||||
*/
|
||||
function synonyms_vocabulary_settings_save($vocabulary, $settings) {
|
||||
// If source of synonyms has changed for this vocabulary, we have to trigger
|
||||
// search re-indexing on all the nodes that reference at least 1 term from
|
||||
// this vocabulary.
|
||||
$previous_settings = synonyms_vocabulary_settings($vocabulary);
|
||||
if (implode('', $settings['synonyms']) != implode('', $previous_settings['synonyms'])) {
|
||||
module_load_include('inc', 'synonyms', 'synonyms.pages');
|
||||
synonyms_reindex_nodes_by_vocabulary($vocabulary);
|
||||
}
|
||||
|
||||
variable_set('synonyms_settings_' . $vocabulary->vid, $settings);
|
||||
|
||||
// Now enforcing each setting.
|
||||
@@ -772,7 +688,7 @@ function synonyms_default_field_ensure() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract instances that are applicapable for being source of synonyms.
|
||||
* Extract instances that are applicable for being source of synonyms.
|
||||
*
|
||||
* Walk through all instances of a vocabulary and extract only valid candidates
|
||||
* for becoming a source of synonyms for the vocabulary terms.
|
||||
@@ -783,15 +699,25 @@ function synonyms_default_field_ensure() {
|
||||
* @return array
|
||||
* Array of instance arrays
|
||||
*/
|
||||
function synonyms_instances_extract_applicapable($vocabulary) {
|
||||
$applicapable_field_types = array_keys(synonyms_extractor_info());
|
||||
$applicapable = array();
|
||||
function synonyms_instances_extract_applicable($vocabulary) {
|
||||
$applicable_field_types = array_keys(synonyms_extractor_info());
|
||||
$applicable = array();
|
||||
$bundle = field_extract_bundle('taxonomy_term', $vocabulary);
|
||||
foreach (field_info_instances('taxonomy_term', $bundle) as $instance) {
|
||||
$field = field_info_field($instance['field_name']);
|
||||
if (in_array($field['type'], $applicapable_field_types)) {
|
||||
$applicapable[] = $instance;
|
||||
if (in_array($field['type'], $applicable_field_types)) {
|
||||
$applicable[] = $instance;
|
||||
}
|
||||
}
|
||||
return $applicapable;
|
||||
return $applicable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_views_api().
|
||||
*/
|
||||
function synonyms_views_api() {
|
||||
return array(
|
||||
'api' => 3,
|
||||
'path' => drupal_get_path('module', 'synonyms') . '/views',
|
||||
);
|
||||
}
|
||||
|
@@ -38,7 +38,7 @@ abstract class SynonymsWebTestCase extends DrupalWebTestCase {
|
||||
* Fully loaded taxonomy vocabulary object
|
||||
*
|
||||
* @return object
|
||||
* Fully loaded taxonomy term object of the last interted term into
|
||||
* Fully loaded taxonomy term object of the last inserted term into
|
||||
* the specified vocabulary
|
||||
*/
|
||||
protected function getLastTerm($vocabulary) {
|
||||
@@ -175,28 +175,28 @@ class SynonymsSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
// Asserting the presence of synonym string.
|
||||
$this->assertText($synonym, 'The synonym string is present on taxonomy term view page');
|
||||
|
||||
// Testing the function synonyms_get_term_synonyms().
|
||||
$synonyms = synonyms_get_term_synonyms($no_synonyms_term);
|
||||
$this->assertTrue(empty($synonyms), 'Successfully called synonyms_get_term_synonyms() for a term without synonyms.');
|
||||
$synonyms = synonyms_get_term_synonyms($one_synonym_term);
|
||||
$this->assertTrue(count($synonyms) == 1 && $synonyms[0]['value'] == $synonym1, 'Sucessfully called synonyms_get_term_synonyms for a term with single synonym.');
|
||||
$synonyms = synonyms_get_term_synonyms($two_synonyms_term);
|
||||
$this->assertTrue(count($synonyms) == 2 && $synonyms[0]['value'] == $synonym1 && $synonyms[1]['value'] == $synonym2, 'Sucessfully called synonyms_get_term_synonyms for a term with 2 synonyms.');
|
||||
// Testing the 'synonyms' property of 'taxonomy_term' entity.
|
||||
$synonyms = synonyms_get_sanitized($no_synonyms_term);
|
||||
$this->assertTrue(empty($synonyms), 'Successfully retrieved synonyms_get_sanitized() for a term without synonyms.');
|
||||
$synonyms = synonyms_get_sanitized($one_synonym_term);
|
||||
$this->assertTrue(count($synonyms) == 1 && $synonyms[0] == $synonym1, 'Successfully retrieved synonyms_get_sanitized() for a term with a single synonym.');
|
||||
$synonyms = synonyms_get_sanitized($two_synonyms_term);
|
||||
$this->assertTrue(count($synonyms) == 2 && $synonyms[0] == $synonym1 && $synonyms[1] == $synonym2, 'Successfully retrieved synonyms_get_sanitized() for a term with 2 synonyms.');
|
||||
|
||||
// Testing the function synonyms_get_term_by_synonym().
|
||||
$tid = synonyms_get_term_by_synonym(drupal_strtoupper($synonym), $this->vocabularies['enabled'], $term_parent->tid);
|
||||
$this->assertEqual($tid, $term->tid, 'Sucessfully looked up term by its synonym.');
|
||||
$this->assertEqual($tid, $term->tid, 'Successfully looked up term by its synonym.');
|
||||
$tid = synonyms_get_term_by_synonym(drupal_strtoupper($name), $this->vocabularies['enabled'], $term_parent->tid);
|
||||
$this->assertEqual($tid, $term->tid, 'Sucessfully looked up term by its name.');
|
||||
$this->assertEqual($tid, $term->tid, 'Successfully looked up term by its name.');
|
||||
// Now submitting a non-existing name.
|
||||
$tid = synonyms_get_term_by_synonym($parent_synonym, $this->vocabularies['enabled'], $term_parent->tid);
|
||||
$this->assertEqual($tid, 0, 'synonyms_get_term_by_synonym() returns 0 if the term is not found (considering $parent parameter).');
|
||||
|
||||
// Testing the function synonyms_add_term_by_synonym().
|
||||
$tid = synonyms_add_term_by_synonym(drupal_strtolower($name), $this->vocabularies['enabled'], $term_parent->tid);
|
||||
$this->assertEqual($tid, $term->tid, 'Sucessfully called synonyms_add_term_by_synonym() on an existing title and no new term was created.');
|
||||
$this->assertEqual($tid, $term->tid, 'Successfully called synonyms_add_term_by_synonym() on an existing title and no new term was created.');
|
||||
$tid = synonyms_add_term_by_synonym(drupal_strtolower($synonym), $this->vocabularies['enabled'], $term_parent->tid);
|
||||
$this->assertEqual($tid, $term->tid, 'Sucessfully called synonyms_add_term_by_synonym() on an existing synonym and no new term was created.');
|
||||
$this->assertEqual($tid, $term->tid, 'Successfully called synonyms_add_term_by_synonym() on an existing synonym and no new term was created.');
|
||||
drupal_static_reset();
|
||||
$tid = synonyms_add_term_by_synonym($parent_synonym, $this->vocabularies['enabled'], $term_parent->tid);
|
||||
$new_term = taxonomy_term_load($tid);
|
||||
@@ -217,13 +217,13 @@ class SynonymsSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
$term = array_pop(entity_load('taxonomy_term', array($term->tid), array(), TRUE));
|
||||
$this->assertFalse(isset($term->{SYNONYMS_DEFAULT_FIELD_NAME}), 'The term no longer has synonyms field after disabling "synonyms" feature for a vocabulary');
|
||||
|
||||
// Testing synonyms_get_term_synonums() function.
|
||||
$synonyms = synonyms_get_term_synonyms($no_synonyms_term);
|
||||
$this->assertTrue(empty($synonyms), 'Successfully called synonyms_get_term_synonyms() on a term without synonyms after disabling "synonyms" feature');
|
||||
$synonyms = synonyms_get_term_synonyms($one_synonym_term);
|
||||
$this->assertTrue(empty($synonyms), 'Successfully called synonyms_get_term_synonyms() on a term with single synonym after disabling "synonyms" feature');
|
||||
$synonyms = synonyms_get_term_synonyms($two_synonyms_term);
|
||||
$this->assertTrue(empty($synonyms), 'Successfully called synonyms_get_term_synonyms() on a term with 2 synonyms after disabling "synonyms" feature');
|
||||
// Testing the 'synonyms' property of 'taxonomy_term' entity.
|
||||
$synonyms = synonyms_get_sanitized($no_synonyms_term);
|
||||
$this->assertTrue(empty($synonyms), 'Successfully retrieved synonyms_get_sanitized() on a term without synonyms after disabling "synonyms" feature');
|
||||
$synonyms = synonyms_get_sanitized($one_synonym_term);
|
||||
$this->assertTrue(empty($synonyms), 'Successfully retrieved synonyms_get_sanitized() on a term with a single synonym after disabling "synonyms" feature');
|
||||
$synonyms = synonyms_get_sanitized($two_synonyms_term);
|
||||
$this->assertTrue(empty($synonyms), 'Successfully retrieved synonyms_get_sanitized() on a term with 2 synonyms after disabling "synonyms" feature');
|
||||
|
||||
$tid = synonyms_get_term_by_synonym(drupal_strtoupper($synonym), $this->vocabularies['enabled']);
|
||||
$this->assertEqual($tid, 0, 'synonyms_get_term_by_synonym() returns 0 after disabling "synonyms" feature for a vocabulary');
|
||||
@@ -247,19 +247,49 @@ class SynonymsSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test "Synonym friednly autocomplete" widget of Synonyms module.
|
||||
* Test "Synonyms friendly autocomplete" widget of Synonyms module.
|
||||
*/
|
||||
class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
|
||||
/**
|
||||
* Array of fully loaded vocabulary entities to be used in this test.
|
||||
*
|
||||
* Array is keyed by the corresponding vocabulary's machine name.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $vocabularies = array(
|
||||
'enabled' => TRUE,
|
||||
'disabled' => FALSE,
|
||||
);
|
||||
|
||||
/**
|
||||
* Array of fully loaded taxonomy term entities to be used in this test.
|
||||
*
|
||||
* Term entities are grouped by machine name of the vocabulary to which they
|
||||
* belong.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $terms = array(
|
||||
'enabled' => array(),
|
||||
'disabled' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
* Entity type to which a term reference field with tested widget is attached.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entity_type = 'node';
|
||||
|
||||
/**
|
||||
* Bundle to which a term reference field with tested widget is attached.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $bundle = 'synonyms_test_content';
|
||||
|
||||
/**
|
||||
* GetInfo method.
|
||||
*/
|
||||
@@ -292,7 +322,7 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
// Creating a test content type.
|
||||
$this->drupalPost('admin/structure/types/add', array(
|
||||
'name' => 'Synonyms Test Content',
|
||||
'type' => 'synonyms_test_content',
|
||||
'type' => $this->bundle,
|
||||
), 'Save content type');
|
||||
|
||||
// Attaching each vocabulary term reference field to the new content type.
|
||||
@@ -313,8 +343,8 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
// Flushing static cache.
|
||||
_field_info_collate_fields(TRUE);
|
||||
|
||||
// Now creating taxonomy tree for each vocabularies.
|
||||
// For synonym-disbaled vocabulary just one term is good enough.
|
||||
// Now creating taxonomy tree for each vocabulary.
|
||||
// For synonym-disabled vocabulary just a few terms is good enough.
|
||||
$name = $this->randomName();
|
||||
$this->drupalPost('admin/structure/taxonomy/disabled/add', array(
|
||||
'name' => $name,
|
||||
@@ -355,7 +385,7 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $name . $this->randomName(),
|
||||
), 'Save');
|
||||
$this->terms['enabled']['name_similar_synonym'] = $this->getLastTerm($this->vocabularies['enabled']);
|
||||
$name = 'simiar_synonyms_';
|
||||
$name = 'similar_synonyms_';
|
||||
$this->drupalPost('admin/structure/taxonomy/enabled/add', array(
|
||||
'name' => $this->randomName(),
|
||||
SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $name . $this->randomName(),
|
||||
@@ -379,8 +409,10 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
/**
|
||||
* Test auto-creation functionality.
|
||||
*
|
||||
* Test the auto-creation funcationality of the synonym friendly autocomplete
|
||||
* widget type.
|
||||
* Test the auto-creation functionality of the synonym friendly autocomplete
|
||||
* widget type. Along the way it tests whether synonyms, submitted into the
|
||||
* widget's textfield are converted into the terms, synonyms of which they
|
||||
* are.
|
||||
*/
|
||||
public function testAutoCreation() {
|
||||
// Trying enabled auto creation.
|
||||
@@ -391,12 +423,13 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
$new_term_name = $this->randomName();
|
||||
$this->drupalPost('node/add/synonyms-test-content', array(
|
||||
'title' => $this->randomName(),
|
||||
'field_synonyms_term_enabled[' . LANGUAGE_NONE . ']' => $this->terms['enabled']['no_synonyms']->name . ', ' . $new_term_name,
|
||||
'field_synonyms_term_enabled[' . LANGUAGE_NONE . ']' => $this->terms['enabled']['no_synonyms']->name . ', ' . $new_term_name . ', ' . $this->terms['enabled']['one_synonym']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value'],
|
||||
), 'Save');
|
||||
$this->assertText($this->terms['enabled']['no_synonyms']->name, 'Existing term was assigned to the new node');
|
||||
$this->assertText($new_term_name, 'Auto created term was assigned to the new node');
|
||||
$this->assertText($new_term_name, 'Auto created term was assigned to the new node when Auto creation is on.');
|
||||
$this->assertText($this->terms['enabled']['one_synonym']->name, 'Submitting a synonym into autocomplete widget results in the term, to which the synonym belongs, being assigned to the just created entity (when Auto creation is on).');
|
||||
$term = $this->getLastTerm($this->vocabularies['enabled']);
|
||||
$this->assertEqual($term->name, $new_term_name, 'The auto created term has been created');
|
||||
$this->assertEqual($term->name, $new_term_name, 'The auto created term has been created when Auto creation is on.');
|
||||
|
||||
// Trying disabled auto creation.
|
||||
$this->drupalPost('admin/structure/types/manage/synonyms-test-content/fields/field_synonyms_term_enabled', array(
|
||||
@@ -406,12 +439,13 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
$new_term_name = $this->randomName();
|
||||
$this->drupalPost('node/add/synonyms-test-content', array(
|
||||
'title' => $this->randomName(),
|
||||
'field_synonyms_term_enabled[und]' => $this->terms['enabled']['no_synonyms']->name . ', ' . $new_term_name,
|
||||
'field_synonyms_term_enabled[' . LANGUAGE_NONE . ']' => $this->terms['enabled']['no_synonyms']->name . ', ' . $new_term_name . ', ' . $this->terms['enabled']['one_synonym']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value'],
|
||||
), 'Save');
|
||||
$this->assertText($this->terms['enabled']['no_synonyms']->name, 'Existing term was assigned to the new node');
|
||||
$this->assertNoText($new_term_name, 'Auto created term was not assigned to the new node');
|
||||
$this->assertNoText($new_term_name, 'Auto created term was not assigned to the new node when Auto creation is off.');
|
||||
$this->assertText($this->terms['enabled']['one_synonym']->name, 'Submitting a synonym into autocomplete widget results in the term, to which the synonym belongs, being assigned to the just created entity (when Auto creation is off).');
|
||||
$term = $this->getLastTerm($this->vocabularies['enabled']);
|
||||
$this->assertNotEqual($term->name, $new_term_name, 'The auto created term has not been created');
|
||||
$this->assertNotEqual($term->name, $new_term_name, 'The auto created term has not been created when Auto creation is off.');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -449,7 +483,7 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'disabled',
|
||||
'input' => $terms['term1']->name . ',' . drupal_strtoupper(drupal_substr($terms['term1']->name, 1, -1)),
|
||||
'response' => array($terms['term1']->name . ', ' . $this->terms['disabled']['term1_longer_name']->name => $this->terms['disabled']['term1_longer_name']->name),
|
||||
'response' => array($terms['term1']->name . ', ' . $terms['term1_longer_name']->name => $terms['term1_longer_name']->name),
|
||||
'message' => 'Submitting one term already chosen along with a name similar to 2 existing term names into a disabled synonyms vocabulary',
|
||||
);
|
||||
$assertions[] = array(
|
||||
@@ -475,7 +509,7 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
);
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => $terms['one_synonym']->name . ',' . $terms['one_synonym']->synonyms_synonyms[LANGUAGE_NONE][0]['safe_value'],
|
||||
'input' => $terms['one_synonym']->name . ',' . $terms['one_synonym']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['safe_value'],
|
||||
'response' => array(),
|
||||
'message' => 'Submitting a synonym of a term over again into an enabled synonyms vocabulary',
|
||||
);
|
||||
@@ -487,36 +521,24 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
'message' => 'Submitting a name similar to ' . $k . ' term into an enabled synonyms vocabulary',
|
||||
);
|
||||
|
||||
$synonyms = field_get_items('taxonomy_term', $terms[$k], 'synonyms_synonyms');
|
||||
$synonyms = field_get_items('taxonomy_term', $terms[$k], SYNONYMS_DEFAULT_FIELD_NAME);
|
||||
if (is_array($synonyms)) {
|
||||
foreach ($synonyms as $delta => $item) {
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => drupal_strtolower(drupal_substr($item['safe_value'], 1, -1)),
|
||||
'response' => array($terms[$k]->name => $this->synonymAutocompleteResult($terms[$k], $item['safe_value'])),
|
||||
'message' => 'Submitting a name similar to synonym#' . $delta . ' of the term ' . $k . 'into an enabled synonyms vocabulary',
|
||||
'message' => 'Submitting a name similar to synonym#' . $delta . ' of the term ' . $k . ' into an enabled synonyms vocabulary',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => $terms['name_similar_synonym']->name,
|
||||
'response' => array($terms['name_similar_synonym']->name => $terms['name_similar_synonym']->name),
|
||||
'message' => 'Submitting a keyword similar to name and synonym of a term into an enabled synonyms vocabulary',
|
||||
);
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => 'simiar_synonyms_',
|
||||
'response' => array($terms['similar_synonyms']->name => $this->synonymAutocompleteResult($terms['similar_synonyms'], $terms['similar_synonyms']->synonyms_synonyms[LANGUAGE_NONE][0]['safe_value'])),
|
||||
'message' => 'Submitting a keyword similar to name and synonym of a term into an enabled synonyms vocabulary',
|
||||
);
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => 'one_term_name_another_synonym_',
|
||||
'response' => array(
|
||||
$terms['name_another_synonym']->name => $terms['name_another_synonym']->name,
|
||||
$terms['synonym_another_name']->name => $this->synonymAutocompleteResult($terms['synonym_another_name'], $terms['synonym_another_name']->synonyms_synonyms[LANGUAGE_NONE][0]['safe_value']),
|
||||
$terms['synonym_another_name']->name => $this->synonymAutocompleteResult($terms['synonym_another_name'], $terms['synonym_another_name']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['safe_value']),
|
||||
),
|
||||
'message' => 'Submitting a keyword similar to name of one term and synonym of another into an enabled synonyms vocabulary yields both included in the results',
|
||||
);
|
||||
@@ -526,11 +548,117 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test 'Suggestions Size' setting of synonyms-friendly autocomplete widget.
|
||||
*/
|
||||
function testWidgetSettingsSuggestionSize() {
|
||||
$suggestion_size = 1;
|
||||
$this->drupalPost('admin/structure/types/manage/synonyms-test-content/fields/field_synonyms_term_disabled', array(
|
||||
'instance[widget][settings][suggestion_size]' => $suggestion_size,
|
||||
), 'Save settings');
|
||||
$this->drupalPost('admin/structure/types/manage/synonyms-test-content/fields/field_synonyms_term_enabled', array(
|
||||
'instance[widget][settings][suggestion_size]' => $suggestion_size,
|
||||
), 'Save settings');
|
||||
|
||||
$assertions = array();
|
||||
|
||||
// If size was bigger than 1, we'd get suggested 2 terms: 'term1' and
|
||||
// 'term1_longer_name'.
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'disabled',
|
||||
'input' => $this->terms['disabled']['term1']->name,
|
||||
'response' => array($this->terms['disabled']['term1']->name => $this->terms['disabled']['term1']->name),
|
||||
'message' => 'Suggestions Size option is respected in autocomplete widget for term suggestion entries.',
|
||||
);
|
||||
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => $this->terms['enabled']['name_similar_synonym']->name,
|
||||
'response' => array($this->terms['enabled']['name_similar_synonym']->name => $this->terms['enabled']['name_similar_synonym']->name),
|
||||
'message' => 'Suggestions Size option is respected in autocomplete widget for term and synonym suggestion entries.'
|
||||
);
|
||||
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => drupal_substr($this->terms['enabled']['similar_synonyms']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value'], 0, 8),
|
||||
'response' => array($this->terms['enabled']['similar_synonyms']->name => $this->synonymAutocompleteResult($this->terms['enabled']['similar_synonyms'], $this->terms['enabled']['similar_synonyms']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value'])),
|
||||
'message' => 'Suggestions Size option is respected in autocomplete widget for synonyms suggestion entries.'
|
||||
);
|
||||
|
||||
foreach ($assertions as $assertion) {
|
||||
$this->assertAutocompleteMenuPath($assertion);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test 'Suggest only one entry per term' setting of autocomplete widget.
|
||||
*/
|
||||
function testWidgetSettingsSuggestOnlyUnique() {
|
||||
$assertions = array();
|
||||
|
||||
// Testing disabled "Suggest only one entry per term" setting.
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => $this->terms['enabled']['name_similar_synonym']->name,
|
||||
'response' => array(
|
||||
$this->terms['enabled']['name_similar_synonym']->name => $this->terms['enabled']['name_similar_synonym']->name,
|
||||
$this->terms['enabled']['name_similar_synonym']->name . ' ' => $this->synonymAutocompleteResult($this->terms['enabled']['name_similar_synonym'], $this->terms['enabled']['name_similar_synonym']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value']),
|
||||
),
|
||||
'message' => 'Both term and its synonym are shown when "Suggest only one entry per term" is off.'
|
||||
);
|
||||
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => drupal_substr($this->terms['enabled']['similar_synonyms']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value'], 0, 8),
|
||||
'response' => array(
|
||||
$this->terms['enabled']['similar_synonyms']->name => $this->synonymAutocompleteResult($this->terms['enabled']['similar_synonyms'], $this->terms['enabled']['similar_synonyms']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value']),
|
||||
$this->terms['enabled']['similar_synonyms']->name . ' ' => $this->synonymAutocompleteResult($this->terms['enabled']['similar_synonyms'], $this->terms['enabled']['similar_synonyms']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][1]['value']),
|
||||
),
|
||||
'message' => 'Multiple synonyms are shown when "Suggest only one entry per term" is off.'
|
||||
);
|
||||
|
||||
foreach ($assertions as $assertion) {
|
||||
$this->assertAutocompleteMenuPath($assertion);
|
||||
}
|
||||
|
||||
// Testing enabled "Suggest only one entry per term" setting.
|
||||
$this->drupalPost('admin/structure/types/manage/synonyms-test-content/fields/field_synonyms_term_disabled', array(
|
||||
'instance[widget][settings][suggest_only_unique]' => TRUE,
|
||||
), 'Save settings');
|
||||
$this->drupalPost('admin/structure/types/manage/synonyms-test-content/fields/field_synonyms_term_enabled', array(
|
||||
'instance[widget][settings][suggest_only_unique]' => TRUE,
|
||||
), 'Save settings');
|
||||
|
||||
$assertions = array();
|
||||
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => $this->terms['enabled']['name_similar_synonym']->name,
|
||||
'response' => array(
|
||||
$this->terms['enabled']['name_similar_synonym']->name => $this->terms['enabled']['name_similar_synonym']->name,
|
||||
),
|
||||
'message' => 'Only term is shown and synonym is not shown when "Suggest only one entry per term" is on.'
|
||||
);
|
||||
|
||||
$assertions[] = array(
|
||||
'vocabulary' => 'enabled',
|
||||
'input' => drupal_substr($this->terms['enabled']['similar_synonyms']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value'], 0, 8),
|
||||
'response' => array(
|
||||
$this->terms['enabled']['similar_synonyms']->name => $this->synonymAutocompleteResult($this->terms['enabled']['similar_synonyms'], $this->terms['enabled']['similar_synonyms']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value']),
|
||||
),
|
||||
'message' => 'Only single synonym is shown when "Suggest only one entry per term" is on.'
|
||||
);
|
||||
|
||||
foreach ($assertions as $assertion) {
|
||||
$this->assertAutocompleteMenuPath($assertion);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert output of synonym friendly autocomplete path.
|
||||
*
|
||||
* @param array $assertion
|
||||
* Specially encoded array of assertion. Should include the follwing keys:
|
||||
* Specially encoded array of assertion. Should include the following keys:
|
||||
* vocabulary - machine name of vocabulary whose field is asserted
|
||||
* input - input string to be fed to autocomplete menu path
|
||||
* response - JSON decoded expected response of autocomplete menu path
|
||||
@@ -538,7 +666,7 @@ class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
* page
|
||||
*/
|
||||
protected function assertAutocompleteMenuPath($assertion) {
|
||||
$response = $this->drupalGet('synonyms/autocomplete/field_synonyms_term_' . $assertion['vocabulary'] . '/' . $assertion['input']);
|
||||
$response = $this->drupalGet('synonyms/autocomplete/field_synonyms_term_' . $assertion['vocabulary'] . '/' . $this->entity_type . '/' . $this->bundle . '/' . $assertion['input']);
|
||||
if (!$response) {
|
||||
$this->fail($assertion['message'], 'Autocomplete Menu Path');
|
||||
return;
|
||||
@@ -635,32 +763,46 @@ class SearchIndexSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
*
|
||||
* Since logically term and its synonyms represent the same entity, the idea
|
||||
* is that searching by a term synonym should trigger all content referencing
|
||||
* that term to be included in search results.
|
||||
* that term to be included in search results. Additionally we test that when
|
||||
* a synonym is deleted/edited in a term, corresponding content is no longer
|
||||
* encountered when searched by ex-synonym.
|
||||
*/
|
||||
public function testSearchTermSynonym() {
|
||||
// Create a few terms and synonyms.
|
||||
$terms = array();
|
||||
$this->drupalPost('admin/structure/taxonomy/enabled/add', array(
|
||||
$term = (object) array(
|
||||
'vid' => $this->vocabularies['enabled']->vid,
|
||||
'name' => $this->randomName(),
|
||||
), 'Save');
|
||||
);
|
||||
taxonomy_term_save($term);
|
||||
$terms['no_synonyms'] = $this->getLastTerm($this->vocabularies['enabled']);
|
||||
$this->drupalPost('admin/structure/taxonomy/enabled/add', array(
|
||||
|
||||
$term = (object) array(
|
||||
'vid' => $this->vocabularies['enabled']->vid,
|
||||
'name' => $this->randomName(),
|
||||
SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(),
|
||||
), 'Save');
|
||||
SYNONYMS_DEFAULT_FIELD_NAME => array(
|
||||
LANGUAGE_NONE => array(
|
||||
array('value' => $this->randomName()),
|
||||
),
|
||||
)
|
||||
);
|
||||
taxonomy_term_save($term);
|
||||
$terms['one_synonym'] = $this->getLastTerm($this->vocabularies['enabled']);
|
||||
$this->drupalPost('admin/structure/taxonomy/enabled/add', array(
|
||||
|
||||
$term = (object) array(
|
||||
'vid' => $this->vocabularies['enabled']->vid,
|
||||
'name' => $this->randomName(),
|
||||
SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(),
|
||||
), 'Add another item');
|
||||
$this->drupalPost(NULL, array(
|
||||
SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][1][value]' => $this->randomName(),
|
||||
), 'Save');
|
||||
SYNONYMS_DEFAULT_FIELD_NAME => array(
|
||||
LANGUAGE_NONE => array(
|
||||
array('value' => $this->randomName()),
|
||||
array('value' => $this->randomName()),
|
||||
),
|
||||
)
|
||||
);
|
||||
taxonomy_term_save($term);
|
||||
$terms['two_synonyms'] = $this->getLastTerm($this->vocabularies['enabled']);
|
||||
|
||||
$assertions = array();
|
||||
|
||||
// Creating a node, which refereces to all the terms we have.
|
||||
// Creating a node, which references to all the terms we have.
|
||||
$title = $this->randomName();
|
||||
$this->drupalPost('node/add/synonyms-test-content', array(
|
||||
'title' => $title,
|
||||
@@ -671,58 +813,76 @@ class SearchIndexSynonymsWebTestCase extends SynonymsWebTestCase {
|
||||
// Rebuilding Search index.
|
||||
$this->cronRun();
|
||||
|
||||
foreach ($terms as $k => $v) {
|
||||
$assertions[] = array(
|
||||
'keyword' => $v->name,
|
||||
'results' => array($node),
|
||||
'message' => 'Searching by name of the term ' . $k,
|
||||
);
|
||||
foreach (synonyms_get_term_synonyms($v) as $delta => $synonym) {
|
||||
$assertions[] = array(
|
||||
'keyword' => $synonym['value'],
|
||||
'results' => array($node),
|
||||
'message' => 'Searching by synonym #' . $delta . ' of the term ' . $k,
|
||||
);
|
||||
foreach ($terms as $k => $term) {
|
||||
$this->assertSearchResults($term->name, array($node), 'Searching by name of the term ' . $k);
|
||||
foreach (synonyms_get_sanitized($term) as $delta => $synonym) {
|
||||
$this->assertSearchResults($synonym, array($node), 'Searching by synonym #' . $delta . ' of the term ' . $k);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($assertions as $assertion) {
|
||||
$this->assertSearchResults($assertion);
|
||||
// Removing a synonym from the term. Then asserting node got re-indexed
|
||||
// with new values of synonyms.
|
||||
$deleted_synonym = array_pop($terms['one_synonym']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE]);
|
||||
taxonomy_term_save($terms['one_synonym']);
|
||||
$this->cronRun();
|
||||
$this->assertSearchResults($deleted_synonym['value'], array(), 'Searching by recently deleted synonym of a taxonomy term yields no results.');
|
||||
|
||||
// Editing a synonym in a term. Then asserting node got re-indexed with new
|
||||
// values of synonyms.
|
||||
$ex_synonym = $terms['two_synonyms']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value'];
|
||||
$terms['two_synonyms']->{SYNONYMS_DEFAULT_FIELD_NAME}[LANGUAGE_NONE][0]['value'] = $this->randomName();
|
||||
taxonomy_term_save($terms['two_synonyms']);
|
||||
$this->cronRun();
|
||||
$this->assertSearchResults($ex_synonym, array(), 'Searching by recently changed synonym of a taxonomy term yields no results.');
|
||||
|
||||
// We disable entire field from being source of synonyms and make sure for
|
||||
// all synonyms search results are empty.
|
||||
$this->drupalPost('admin/structure/taxonomy/enabled/edit', array(
|
||||
'synonyms[synonyms][' . SYNONYMS_DEFAULT_FIELD_NAME . ']' => FALSE,
|
||||
), 'Save');
|
||||
$this->cronRun();
|
||||
foreach ($terms as $k => $term) {
|
||||
$items = field_get_items('taxonomy_term', $term, SYNONYMS_DEFAULT_FIELD_NAME);
|
||||
if (is_array($items)) {
|
||||
foreach ($items as $synonym) {
|
||||
$this->assertSearchResults($synonym['value'], array(), 'Searching by ' . $k . ' term synonym, which field was recently disabled as source of synonyms in vocabulary yields no results.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert search results.
|
||||
*
|
||||
* @param array $assertion
|
||||
* Specially encoded array of assertion. Should include the follwing keys:
|
||||
* keyword - what keyword has to be supplied to the search mechanism
|
||||
* results - array of nodes that are expected to be on search results
|
||||
* message - Drupal assertion message to be displayed on test results
|
||||
* page
|
||||
* @param $keyword string
|
||||
* Keyword to supply to the search mechanism
|
||||
* @param $results array
|
||||
* Array of fully loaded nodes that are expected to be on search results
|
||||
* @param $message string
|
||||
* Drupal assertion message to display on test results page
|
||||
*/
|
||||
protected function assertSearchResults($assertion) {
|
||||
$response = $this->drupalGet('search/node/' . $assertion['keyword']);
|
||||
protected function assertSearchResults($keyword, $results, $message) {
|
||||
$response = $this->drupalGet('search/node/' . $keyword);
|
||||
$matches = array();
|
||||
preg_match_all('#\<li[^>]+class="search-result"[^>]*\>(.*?)\</li\>#si', $response, $matches);
|
||||
$results = $matches[1];
|
||||
if (count($results) != count($assertion['results'])) {
|
||||
$this->fail($assertion['message']);
|
||||
$matches = $matches[1];
|
||||
if (count($matches) != count($results)) {
|
||||
$this->fail($message);
|
||||
return;
|
||||
}
|
||||
$results = implode('', $results);
|
||||
foreach ($assertion['results'] as $node) {
|
||||
if (strpos($results, 'node/' . $node->nid) === FALSE) {
|
||||
$this->fail($assertion['message']);
|
||||
$matches = implode('', $matches);
|
||||
foreach ($results as $node) {
|
||||
if (strpos($matches, 'node/' . $node->nid) === FALSE) {
|
||||
$this->fail($message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
$this->pass($assertion['message']);
|
||||
$this->pass($message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for all test cases that test Synonoyms Extractor classes.
|
||||
* Base class for all test cases that test Synonyms Extractor classes.
|
||||
*/
|
||||
abstract class AbstractSynonymsExtractorWebTestCase extends DrupalWebTestCase {
|
||||
|
||||
@@ -769,7 +929,7 @@ abstract class AbstractSynonymsExtractorWebTestCase extends DrupalWebTestCase {
|
||||
parent::setUp($modules);
|
||||
|
||||
$this->vocabulary = (object) array(
|
||||
'name' => 'Test Synonyms Extactor',
|
||||
'name' => 'Test Synonyms Extractor',
|
||||
'machine_name' => 'synonyms_extractor',
|
||||
);
|
||||
taxonomy_vocabulary_save($this->vocabulary);
|
||||
@@ -787,7 +947,7 @@ abstract class AbstractSynonymsExtractorWebTestCase extends DrupalWebTestCase {
|
||||
* @param array $field
|
||||
* Field definition array as expected by Field API
|
||||
* @param array $instance
|
||||
* Instance defintion array as expected by Field API
|
||||
* Instance definition array as expected by Field API
|
||||
*/
|
||||
protected function addFieldInstance($field, $instance) {
|
||||
$field = field_create_field($field);
|
||||
@@ -821,7 +981,7 @@ abstract class AbstractSynonymsExtractorWebTestCase extends DrupalWebTestCase {
|
||||
taxonomy_term_save($term);
|
||||
$items = field_get_items('taxonomy_term', $term, $this->field['field_name']);
|
||||
|
||||
$synonyms = is_array($items) ? call_user_func($this->extractor . '::synonymsExtract', $items, $this->field, $this->instance, $term, 'taxonomy_term') : array();
|
||||
$synonyms = is_array($items) ? call_user_func(array($this->extractor, 'synonymsExtract'), $items, $this->field, $this->instance, $term, 'taxonomy_term') : array();
|
||||
$this->assertTrue(count(array_intersect($etalon, $synonyms)) == count($etalon), 'Synonyms Extractor ' . $this->extractor . ' passed synonymsExtract() method: ' . $message);
|
||||
// Cleaning up.
|
||||
taxonomy_term_delete($term->tid);
|
||||
@@ -862,7 +1022,7 @@ abstract class AbstractSynonymsExtractorWebTestCase extends DrupalWebTestCase {
|
||||
$efq = new EntityFieldQuery();
|
||||
$efq->entityCondition('entity_type', 'taxonomy_term')
|
||||
->entityCondition('bundle', $this->vocabulary->machine_name);
|
||||
call_user_func($this->extractor . '::processEntityFieldQuery', $tag, $efq, $this->field, $this->instance);
|
||||
call_user_func(array($this->extractor, 'processEntityFieldQuery'), $tag, $efq, $this->field, $this->instance);
|
||||
$result = $efq->execute();
|
||||
$result = isset($result['taxonomy_term']) ? array_keys($result['taxonomy_term']) : array();
|
||||
// Asserting results of EntityFieldQuery.
|
||||
@@ -894,7 +1054,7 @@ abstract class AbstractSynonymsExtractorWebTestCase extends DrupalWebTestCase {
|
||||
* SimpleTest assertion method
|
||||
*/
|
||||
protected function assertMergeEntityAsSynonym($items, $synonym_entity, $synonym_entity_type, $etalon, $message = '') {
|
||||
$extra_items = call_user_func($this->extractor . '::mergeEntityAsSynonym', $items, $this->field, $this->instance, $synonym_entity, $synonym_entity_type);
|
||||
$extra_items = call_user_func(array($this->extractor, 'mergeEntityAsSynonym'), $items, $this->field, $this->instance, $synonym_entity, $synonym_entity_type);
|
||||
foreach ($etalon as $k => $v) {
|
||||
if (count(array_intersect($etalon[$k], $extra_items[$k])) != count($etalon[$k])) {
|
||||
$this->fail('Synonyms Extractor ' . $this->extractor . ' passed mergeEntityAsSynonym() method: ' . $message);
|
||||
@@ -908,7 +1068,7 @@ abstract class AbstractSynonymsExtractorWebTestCase extends DrupalWebTestCase {
|
||||
/**
|
||||
* Test SynonymsSynonymsExtractor class.
|
||||
*/
|
||||
class SynonymsSynonoymsExtractorWebTestCase extends AbstractSynonymsExtractorWebTestCase {
|
||||
class SynonymsSynonymsExtractorWebTestCase extends AbstractSynonymsExtractorWebTestCase {
|
||||
|
||||
/**
|
||||
* GetInfo method.
|
||||
@@ -995,7 +1155,7 @@ class SynonymsSynonoymsExtractorWebTestCase extends AbstractSynonymsExtractorWeb
|
||||
/**
|
||||
* Test TaxonomySynonymsExtractor class.
|
||||
*/
|
||||
class TaxonomySynonoymsExtractorWebTestCase extends AbstractSynonymsExtractorWebTestCase {
|
||||
class TaxonomySynonymsExtractorWebTestCase extends AbstractSynonymsExtractorWebTestCase {
|
||||
|
||||
/**
|
||||
* Taxonomy vocabulary object terms.
|
||||
|
Reference in New Issue
Block a user