summaryrefslogtreecommitdiff
path: root/modules/simpletest
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2008-11-02 14:42:45 +0000
committerDries Buytaert <dries@buytaert.net>2008-11-02 14:42:45 +0000
commitbd22265b458032ad71f22bb471ed69995874d37c (patch)
tree84845e787d71ff61476169af0aae228e1ebafc46 /modules/simpletest
parentaeaeb17c73c2f29b87b5d4504834d2e158d00883 (diff)
downloadbrdo-bd22265b458032ad71f22bb471ed69995874d37c.tar.gz
brdo-bd22265b458032ad71f22bb471ed69995874d37c.tar.bz2
- Patch #306224 by catch et al: improving the taxonomy hook system.
Diffstat (limited to 'modules/simpletest')
-rw-r--r--modules/simpletest/tests/taxonomy_test.info9
-rw-r--r--modules/simpletest/tests/taxonomy_test.install54
-rw-r--r--modules/simpletest/tests/taxonomy_test.module72
-rw-r--r--modules/simpletest/tests/taxonomy_test.test59
4 files changed, 194 insertions, 0 deletions
diff --git a/modules/simpletest/tests/taxonomy_test.info b/modules/simpletest/tests/taxonomy_test.info
new file mode 100644
index 000000000..8206e22fd
--- /dev/null
+++ b/modules/simpletest/tests/taxonomy_test.info
@@ -0,0 +1,9 @@
+; $Id$
+name = "Taxonomy test module"
+description = "Tests functions and hooks not used in core".
+package = Testing
+version = VERSION
+core = 7.x
+files[] = taxonomy_test.module
+hidden[] = TRUE
+dependencies[] = Taxonomy
diff --git a/modules/simpletest/tests/taxonomy_test.install b/modules/simpletest/tests/taxonomy_test.install
new file mode 100644
index 000000000..770fda6d5
--- /dev/null
+++ b/modules/simpletest/tests/taxonomy_test.install
@@ -0,0 +1,54 @@
+<?php
+// $Id$
+
+/**
+ * Implementation of hook_schema().
+ */
+function taxonomy_test_schema() {
+ $schema['term_antonym'] = array(
+ 'description' => t('Stores term antonyms.'),
+ 'fields' => array(
+ 'taid' => array(
+ 'type' => 'serial',
+ 'not null' => TRUE,
+ 'description' => t('Primary Key: Unique term antonym ID.'),
+ ),
+ 'tid' => array(
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ 'default' => 0,
+ 'description' => t('The {term_data}.tid of the term.'),
+ ),
+ 'name' => array(
+ 'type' => 'varchar',
+ 'length' => 255,
+ 'not null' => TRUE,
+ 'default' => '',
+ 'description' => t('The name of the antonym.'),
+ ),
+ ),
+ 'indexes' => array(
+ 'tid' => array('tid'),
+ 'name_tid' => array('name', 'tid'),
+ ),
+ 'primary key' => array('taid'),
+ );
+
+ return $schema;
+}
+
+/**
+ * Implementation of hook_install().
+ */
+function taxonomy_test_install() {
+ drupal_install_schema('taxonomy_test');
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function taxonomy_test_uninstall() {
+ drupal_uninstall_schema('taxonomy_test');
+}
+
diff --git a/modules/simpletest/tests/taxonomy_test.module b/modules/simpletest/tests/taxonomy_test.module
new file mode 100644
index 000000000..7f5b6f4df
--- /dev/null
+++ b/modules/simpletest/tests/taxonomy_test.module
@@ -0,0 +1,72 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Test module for Taxonomy hooks and functions not used in core.
+ */
+
+/**
+ * Implementation of hook_taxonomy_term_load().
+ */
+function taxonomy_test_taxonomy_term_load($term) {
+ $term->antonyms = taxonomy_test_get_antonyms($term->tid);
+}
+
+/**
+ * Implementation of hook_taxonomy_term_save().
+ */
+function taxonomy_test_taxonomy_term_save($term) {
+ taxonomy_test_taxonomy_term_delete($term);
+ if (!empty($term->antonyms)) {
+ foreach (explode ("\n", str_replace("\r", '', $term->antonyms)) as $antonym) {
+ if ($antonym) {
+ db_insert('term_antonym')
+ ->fields(array(
+ 'tid' => $term->tid,
+ 'name' => rtrim($antonym),
+ ))
+ ->execute();
+ }
+ }
+ }
+}
+
+/**
+ * Implementation of hook_taxonomy_term_delete().
+ */
+function taxonomy_test_taxonomy_term_delete($term) {
+ db_delete('term_antonym')->condition('tid', $term->tid)->execute();
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+function taxonomy_test_form_alter(&$form, $form_state, $form_id) {
+ if ($form_id == 'taxonomy_form_term') {
+ $antonyms = taxonomy_test_get_antonyms($form['#term']['tid']);
+ $form['advanced']['antonyms'] = array(
+ '#type' => 'textarea',
+ '#title' => t('Antonyms'),
+ '#default_value' => !empty($antonyms) ? implode("\n", $antonyms) : NULL,
+ '#description' => t('Antonyms of this term, one antonym per line.')
+ );
+ }
+}
+
+/**
+ * Return an array of antonyms of the given term ID.
+ */
+function taxonomy_test_get_antonyms($tid) {
+ if ($tid) {
+ $antonyms = array();
+ $result = db_query('SELECT name FROM {term_antonym} WHERE tid = :tid', array(':tid' => $tid));
+ foreach($result as $antonym) {
+ $antonyms[] = $antonym->name;
+ }
+ return $antonyms;
+ }
+ else {
+ return FALSE;
+ }
+}
diff --git a/modules/simpletest/tests/taxonomy_test.test b/modules/simpletest/tests/taxonomy_test.test
new file mode 100644
index 000000000..469c3e693
--- /dev/null
+++ b/modules/simpletest/tests/taxonomy_test.test
@@ -0,0 +1,59 @@
+<?php
+// $Id$
+
+class TaxonomyHooksTestCase extends DrupalWebTestCase {
+ /**
+ * Implementation of getInfo().
+ */
+ function getInfo() {
+ return array(
+ 'name' => t('Taxonomy term hooks'),
+ 'description' => t('Hooks for taxonomy term load/save/delete.'),
+ 'group' => t('Taxonomy')
+ );
+ }
+
+ /**
+ * Implementation of setUp().
+ */
+ function setUp() {
+ parent::setUp('taxonomy', 'taxonomy_test');
+ $taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
+ $this->drupalLogin($taxonomy_admin);
+ }
+
+ /**
+ * Test that hooks are run correctly on creating, editing and deleting a term.
+ */
+ function testTaxonomyTermHooks() {
+ // Create a taxonomy vocabulary.
+ $edit = array(
+ 'name' => $this->randomName(),
+ );
+ $this->drupalPost('admin/content/taxonomy/add', $edit, t('Save'));
+
+ // Create a term with one antonym.
+ $edit = array(
+ 'name' => $this->randomName(),
+ 'antonyms' => 'Long',
+ );
+ $this->drupalPost('admin/content/taxonomy/1/add', $edit, t('Save'));
+ $terms = taxonomy_get_term_by_name($edit['name']);
+ $term = taxonomy_term_load($terms[0]->tid);
+ $this->assertEqual($term->antonyms[0], $edit['antonyms'], t('Antonyms were loaded into the term object'));
+
+ // Update the term with a different antonym.
+ $edit = array(
+ 'name' => $this->randomName(),
+ 'antonyms' => 'Short',
+ );
+ $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
+ $term = taxonomy_term_load($term->tid, TRUE);
+ $this->assertTrue(in_array($edit['antonyms'], $term->antonyms), t('Antonym was successfully edited'));
+
+ // Delete the term.
+ taxonomy_del_term($term->tid);
+ $antonyms = db_query('SELECT taid FROM {term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField();
+ $this->assertFalse($antonyms, t('The antonyms were deleted from the database.'));
+ }
+}