#537044 by dropcube: Simplify and expand taxonomy hook tests.

merge-requests/26/head
Angie Byron 2009-08-03 20:19:29 +00:00
parent a1202e744f
commit 2e34decb3b
3 changed files with 41 additions and 62 deletions

View File

@ -10,14 +10,9 @@
* Implement hook_schema(). * Implement hook_schema().
*/ */
function taxonomy_test_schema() { function taxonomy_test_schema() {
$schema['term_antonym'] = array( $schema['taxonomy_term_antonym'] = array(
'description' => 'Stores term antonyms.', 'description' => 'Stores term antonym.',
'fields' => array( 'fields' => array(
'taid' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique term antonym ID.',
),
'tid' => array( 'tid' => array(
'type' => 'int', 'type' => 'int',
'unsigned' => TRUE, 'unsigned' => TRUE,
@ -33,11 +28,7 @@ function taxonomy_test_schema() {
'description' => 'The name of the antonym.', 'description' => 'The name of the antonym.',
), ),
), ),
'indexes' => array( 'primary key' => array('tid'),
'tid' => array('tid'),
'name_tid' => array('name', 'tid'),
),
'primary key' => array('taid'),
); );
return $schema; return $schema;

View File

@ -11,7 +11,10 @@
*/ */
function taxonomy_test_taxonomy_term_load(&$terms) { function taxonomy_test_taxonomy_term_load(&$terms) {
foreach ($terms as $term) { foreach ($terms as $term) {
$term->antonyms = taxonomy_test_get_antonyms($term->tid); $antonym = taxonomy_test_get_antonym($term->tid);
if ($antonym) {
$term->antonym = $antonym;
}
} }
} }
@ -19,44 +22,35 @@ function taxonomy_test_taxonomy_term_load(&$terms) {
* Implement hook_taxonomy_term_insert(). * Implement hook_taxonomy_term_insert().
*/ */
function taxonomy_test_taxonomy_term_insert($term) { function taxonomy_test_taxonomy_term_insert($term) {
if (!empty($term->antonyms)) { if (!empty($term->antonym)) {
foreach (explode ("\n", str_replace("\r", '', $term->antonyms)) as $antonym) { db_insert('taxonomy_term_antonym')
if ($antonym) {
db_insert('term_antonym')
->fields(array( ->fields(array(
'tid' => $term->tid, 'tid' => $term->tid,
'name' => rtrim($antonym), 'name' => trim($term->antonym)
)) ))
->execute(); ->execute();
} }
}
}
} }
/** /**
* Implement hook_taxonomy_term_update(). * Implement hook_taxonomy_term_update().
*/ */
function taxonomy_test_taxonomy_term_update($term) { function taxonomy_test_taxonomy_term_update($term) {
taxonomy_test_taxonomy_term_delete($term); if (!empty($term->antonym)) {
if (!empty($term->antonyms)) { db_merge('taxonomy_term_antonym')
foreach (explode ("\n", str_replace("\r", '', $term->antonyms)) as $antonym) { ->key(array('tid' => $term->tid))
if ($antonym) {
db_insert('term_antonym')
->fields(array( ->fields(array(
'tid' => $term->tid, 'name' => trim($term->antonym)
'name' => rtrim($antonym),
)) ))
->execute(); ->execute();
} }
}
}
} }
/** /**
* Implement hook_taxonomy_term_delete(). * Implement hook_taxonomy_term_delete().
*/ */
function taxonomy_test_taxonomy_term_delete($term) { function taxonomy_test_taxonomy_term_delete($term) {
db_delete('term_antonym') db_delete('taxonomy_term_antonym')
->condition('tid', $term->tid) ->condition('tid', $term->tid)
->execute(); ->execute();
} }
@ -66,29 +60,23 @@ function taxonomy_test_taxonomy_term_delete($term) {
*/ */
function taxonomy_test_form_alter(&$form, $form_state, $form_id) { function taxonomy_test_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'taxonomy_form_term') { if ($form_id == 'taxonomy_form_term') {
$antonyms = taxonomy_test_get_antonyms($form['#term']['tid']); $antonym = taxonomy_test_get_antonym($form['#term']['tid']);
$form['advanced']['antonyms'] = array( $form['advanced']['antonym'] = array(
'#type' => 'textarea', '#type' => 'textfield',
'#title' => t('Antonyms'), '#title' => t('Antonym'),
'#default_value' => !empty($antonyms) ? implode("\n", $antonyms) : NULL, '#default_value' => !empty($antonym) ? $antonym : '',
'#description' => t('Antonyms of this term, one antonym per line.') '#description' => t('Antonym of this term.')
); );
} }
} }
/** /**
* Return an array of antonyms of the given term ID. * Return the antonym of the given term ID.
*/ */
function taxonomy_test_get_antonyms($tid) { function taxonomy_test_get_antonym($tid) {
if ($tid) { return db_select('taxonomy_term_antonym', 'ta')
$antonyms = array(); ->fields('ta', array('name'))
$result = db_query('SELECT name FROM {term_antonym} WHERE tid = :tid', array(':tid' => $tid)); ->condition('tid', $tid)
foreach($result as $antonym) { ->execute()
$antonyms[] = $antonym->name; ->fetchField();
}
return $antonyms;
}
else {
return FALSE;
}
} }

View File

@ -705,26 +705,26 @@ class TaxonomyHooksTestCase extends TaxonomyWebTestCase {
// Create a term with one antonym. // Create a term with one antonym.
$edit = array( $edit = array(
'name' => $this->randomName(), 'name' => $this->randomName(),
'antonyms' => 'Long', 'antonym' => 'Long',
); );
$this->drupalPost('admin/structure/taxonomy/' . $vocabulary->vid . '/add', $edit, t('Save')); $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->vid . '/add', $edit, t('Save'));
$term = reset(taxonomy_get_term_by_name($edit['name'])); $term = reset(taxonomy_get_term_by_name($edit['name']));
$this->assertEqual($term->antonyms[0], $edit['antonyms'], t('Antonyms were loaded into the term object')); $this->assertEqual($term->antonym, $edit['antonym'], t('Antonym was loaded into the term object'));
// Update the term with a different antonym. // Update the term with a different antonym.
$edit = array( $edit = array(
'name' => $this->randomName(), 'name' => $this->randomName(),
'antonyms' => 'Short', 'antonym' => 'Short',
); );
$this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save')); $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
taxonomy_terms_static_reset(); taxonomy_terms_static_reset();
$term = taxonomy_term_load($term->tid); $term = taxonomy_term_load($term->tid);
$this->assertTrue(in_array($edit['antonyms'], $term->antonyms), t('Antonym was successfully edited')); $this->assertEqual($edit['antonym'], $term->antonym, t('Antonym was successfully edited'));
// Delete the term. // Delete the term.
taxonomy_term_delete($term->tid); taxonomy_term_delete($term->tid);
$antonyms = db_query('SELECT taid FROM {term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField(); $antonym = db_query('SELECT tid FROM {taxonomy_term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField();
$this->assertFalse($antonyms, t('The antonyms were deleted from the database.')); $this->assertFalse($antonym, t('The antonym were deleted from the database.'));
} }
} }