Commit 4ec9347a authored by alexpott's avatar alexpott

Issue #1962806 by mtift: Remove Drupal\taxonomy\Tests\HooksTest and taxonomy_test().module.

parent 68ee4617
name: 'Taxonomy test module'
type: module
description: '"Tests functions and hooks not used in core".'
package: Testing
version: VERSION
core: 8.x
hidden: true
dependencies:
- taxonomy
<?php
/**
* @file
* Install, update and uninstall functions for the taxonomy_test module.
*/
/**
* Implements hook_schema().
*/
function taxonomy_test_schema() {
$schema['taxonomy_term_antonym'] = array(
'description' => 'Stores term antonym.',
'fields' => array(
'tid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The {taxonomy_term_data}.tid of the term.',
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The name of the antonym.',
),
),
'primary key' => array('tid'),
);
return $schema;
}
<?php
/**
* @file
* Test module for Taxonomy hooks and functions not used in core.
*
* @see Drupal\taxonomy\Tests\TaxonomyHooksTestCase::testTaxonomyTermHooks()
*/
use Drupal\taxonomy\Plugin\Core\Entity\Term;
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
/**
* Implements hook_entity_field_alter()
*/
function taxonomy_test_entity_field_info_alter(&$info, $entity_type) {
$info['definitions']['antonym'] = array(
'label' => t('Antonym'),
'description' => t('The term antonym.'),
'type' => 'string_field',
'computed' => TRUE,
);
}
/**
* Implements hook_taxonomy_term_load().
*/
function taxonomy_test_taxonomy_term_load(array $terms) {
foreach ($terms as $term) {
$antonym = taxonomy_test_get_antonym($term->id());
if ($antonym) {
$term->antonym->value = $antonym;
}
}
}
/**
* Implements hook_taxonomy_term_insert().
*/
function taxonomy_test_taxonomy_term_insert(Term $term) {
if (!empty($term->antonym->value)) {
db_insert('taxonomy_term_antonym')
->fields(array(
'tid' => $term->id(),
'name' => trim($term->antonym->value)
))
->execute();
}
}
/**
* Implements hook_taxonomy_term_update().
*/
function taxonomy_test_taxonomy_term_update(Term $term) {
if (!empty($term->antonym->value)) {
db_merge('taxonomy_term_antonym')
->key(array('tid' => $term->id()))
->fields(array(
'name' => trim($term->antonym->value)
))
->execute();
}
}
/**
* Implements hook_taxonomy_term_delete().
*/
function taxonomy_test_taxonomy_term_delete(Term $term) {
db_delete('taxonomy_term_antonym')
->condition('tid', $term->id())
->execute();
}
/**
* Implements hook_taxonomy_term_view().
*/
function taxonomy_test_taxonomy_term_view(Term $term, EntityDisplay $display, $view_mode, $langcode) {
if ($view_mode == 'full') {
$term->content['taxonomy_test_term_view_check'] = array(
'#prefix' => '<div>',
'#markup' => t('The antonym is %antonym', array('%antonym' => $term->antonym->value)),
'#suffix' => '</div>',
'#weight' => 10,
);
}
}
/**
* Implements hook_entity_view().
*/
function taxonomy_test_entity_view($entity, EntityDisplay $display, $view_mode, $langcode) {
if ($entity->entityType() == 'taxonomy_term' && $view_mode == 'full') {
$entity->content['taxonomy_test_entity_view_check'] = array(
'#prefix' => '<div>',
'#markup' => t('The antonym is %antonym', array('%antonym' => $entity->antonym->value)),
'#suffix' => '</div>',
'#weight' => 20,
);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function taxonomy_test_form_taxonomy_term_form_alter(&$form, $form_state, $form_id) {
$term = $form_state['controller']->getEntity();
$antonym = taxonomy_test_get_antonym($term->id());
$form['advanced']['antonym'] = array(
'#type' => 'textfield',
'#title' => t('Antonym'),
'#default_value' => !empty($antonym) ? $antonym : '',
'#description' => t('Antonym of this term.')
);
}
/**
* Return the antonym of the given term ID.
*/
function taxonomy_test_get_antonym($tid) {
return db_select('taxonomy_term_antonym', 'ta')
->fields('ta', array('name'))
->condition('tid', $tid)
->execute()
->fetchField();
}
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\HooksTest.
*/
namespace Drupal\taxonomy\Tests;
/**
* Tests for taxonomy hook invocation.
*/
class HooksTest extends TaxonomyTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('taxonomy_test');
public static function getInfo() {
return array(
'name' => 'Taxonomy term hooks',
'description' => 'Hooks for taxonomy term load/save/delete.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp();
$taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
$this->drupalLogin($taxonomy_admin);
}
/**
* Test hooks on CRUD of terms.
*
* Test that hooks are run correctly on creating, editing, viewing,
* and deleting a term.
*
* @see taxonomy_test.module
*/
function testTaxonomyTermHooks() {
$vocabulary = $this->createVocabulary();
// Create a term with one antonym.
$edit = array(
'name' => $this->randomName(),
'antonym' => 'Long',
);
$this->drupalPost('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add', $edit, t('Save'));
$terms = taxonomy_term_load_multiple_by_name($edit['name']);
$term = reset($terms);
$this->assertEqual($term->antonym->value, $edit['antonym'], 'Antonym was loaded into the term object.');
// Update the term with a different antonym.
$edit = array(
'name' => $this->randomName(),
'antonym' => 'Short',
);
$this->drupalPost('taxonomy/term/' . $term->id() . '/edit', $edit, t('Save'));
taxonomy_terms_static_reset();
$term = taxonomy_term_load($term->id());
$this->assertEqual($edit['antonym'], $term->antonym->value, 'Antonym was successfully edited.');
// View the term and ensure that hook_taxonomy_term_view() and
// hook_entity_view() are invoked.
$term = taxonomy_term_load($term->id());
module_load_include('inc', 'taxonomy', 'taxonomy.pages');
$term_build = taxonomy_term_page($term);
$this->assertFalse(empty($term_build['taxonomy_terms'][$term->id()]['taxonomy_test_term_view_check']), 'hook_taxonomy_term_view() was invoked when viewing the term.');
$this->assertFalse(empty($term_build['taxonomy_terms'][$term->id()]['taxonomy_test_entity_view_check']), 'hook_entity_view() was invoked when viewing the term.');
// Delete the term.
$term->delete();
$antonym = db_query('SELECT tid FROM {taxonomy_term_antonym} WHERE tid = :tid', array(':tid' => $term->id()))->fetchField();
$this->assertFalse($antonym, 'The antonym were deleted from the database.');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment