diff options
author | webchick <webchick@24967.no-reply.drupal.org> | 2011-04-24 01:28:19 -0700 |
---|---|---|
committer | webchick <webchick@24967.no-reply.drupal.org> | 2011-04-24 01:28:19 -0700 |
commit | 4d3217b4db094d4808fe8b0c3caea6e3b10dd575 (patch) | |
tree | 9932339a9c1e0e2b91349b96fa76c502a9cc4ad9 | |
parent | 86c5f34ccca03ff76d996c103d3991885f277b90 (diff) | |
download | brdo-4d3217b4db094d4808fe8b0c3caea6e3b10dd575.tar.gz brdo-4d3217b4db094d4808fe8b0c3caea6e3b10dd575.tar.bz2 |
Issue #1003308 by bfroehle: Fix upgrade path for Forum module by hard-coding vocabulary machine name (with tests).
-rw-r--r-- | modules/forum/forum.install | 90 | ||||
-rw-r--r-- | modules/forum/forum.module | 5 | ||||
-rw-r--r-- | modules/simpletest/simpletest.info | 1 | ||||
-rw-r--r-- | modules/simpletest/tests/upgrade/drupal-6.forum.database.php | 261 | ||||
-rw-r--r-- | modules/simpletest/tests/upgrade/upgrade.forum.test | 61 |
5 files changed, 412 insertions, 6 deletions
diff --git a/modules/forum/forum.install b/modules/forum/forum.install index 1813567ae..7da4acf4c 100644 --- a/modules/forum/forum.install +++ b/modules/forum/forum.install @@ -46,7 +46,7 @@ function forum_enable() { // Create the 'taxonomy_forums' field if it doesn't already exist. if (!field_info_field('taxonomy_forums')) { $field = array( - 'field_name' => 'taxonomy_' . $vocabulary->machine_name, + 'field_name' => 'taxonomy_forums', 'type' => 'taxonomy_term_reference', 'settings' => array( 'allowed_values' => array( @@ -73,7 +73,7 @@ function forum_enable() { // Create the instance on the bundle. $instance = array( - 'field_name' => 'taxonomy_' . $vocabulary->machine_name, + 'field_name' => 'taxonomy_forums', 'entity_type' => 'node', 'label' => $vocabulary->name, 'bundle' => 'forum', @@ -338,3 +338,89 @@ function forum_update_7002() { db_drop_index('forum_index', 'forum_topics'); db_add_index('forum_index', 'forum_topics', array('nid', 'tid', 'sticky', 'last_comment_timestamp')); } + +/** + * @addtogroup updates-7.x-extra + * @{ + */ + +/** + * Rename field to 'taxonomy_forums'. + */ +function forum_update_7003() { + $messages = array(); + + $new_field_name = 'taxonomy_forums'; + + // Test to see if the taxonomy_forums field exists. + $fields = _update_7000_field_read_fields(array('field_name' => $new_field_name)); + if ($fields) { + // Since the field exists, we're done. + return; + } + + // Calculate the old field name. + $vid = variable_get('forum_nav_vocabulary', 0); + $vocabulary_machine_name = db_select('taxonomy_vocabulary', 'tv') + ->fields('tv', array('machine_name')) + ->condition('vid', $vid) + ->execute() + ->fetchField(); + $old_field_name = 'taxonomy_' . $vocabulary_machine_name; + + // Read the old fields. + $old_fields = _update_7000_field_read_fields(array('field_name' => $old_field_name)); + foreach ($old_fields as $old_field) { + if ($old_field['storage']['type'] != 'field_sql_storage') { + $messages[] = t('Cannot rename field %id (%old_field_name) to %new_field_name because it does not use the field_sql_storage storage type.', array( + '%id' => $old_field['id'], + '%old_field_name' => $old_field_name, + '%new_field_name' => $new_field_name, + )); + continue; + } + + // Update {field_config}. + db_update('field_config') + ->fields(array('field_name' => $new_field_name)) + ->condition('id', $old_field['id']) + ->execute(); + + // Update {field_config_instance}. + db_update('field_config_instance') + ->fields(array('field_name' => $new_field_name)) + ->condition('field_id', $old_field['id']) + ->execute(); + + // The tables that need updating in the form 'old_name' => 'new_name'. + $tables = array( + 'field_data_' . $old_field_name => 'field_data_' . $new_field_name, + 'field_revision_' . $old_field_name => 'field_revision_' . $new_field_name, + ); + foreach ($tables as $old_table => $new_table) { + $old_column_name = $old_field_name . '_tid'; + $new_column_name = $new_field_name . '_tid'; + + // Rename the column. + db_drop_index($old_table, $old_column_name); + db_change_field($old_table, $old_column_name, $new_column_name, array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + )); + db_drop_index($old_table, $new_column_name); + db_add_index($old_table, $new_column_name, array($new_column_name)); + + // Rename the table. + db_rename_table($old_table, $new_table); + } + } + + cache_clear_all('*', 'cache_field', TRUE); + + return $messages; +} + +/** + * @} End of "addtogroup updates-7.x-extra" + */ diff --git a/modules/forum/forum.module b/modules/forum/forum.module index 273b3e7a0..c58b5c905 100644 --- a/modules/forum/forum.module +++ b/modules/forum/forum.module @@ -169,12 +169,9 @@ function forum_menu_local_tasks_alter(&$data, $router_item, $root_path) { $tid = (isset($router_item['page_arguments'][0]) ? $router_item['page_arguments'][0]->tid : 0); $forum_term = forum_forum_load($tid); if ($forum_term) { - $vid = variable_get('forum_nav_vocabulary', 0); - $vocabulary = taxonomy_vocabulary_load($vid); - $links = array(); // Loop through all bundles for forum taxonomy vocabulary field. - $field = field_info_field('taxonomy_' . $vocabulary->machine_name); + $field = field_info_field('taxonomy_forums'); foreach ($field['bundles']['node'] as $type) { if (node_access('create', $type)) { $links[$type] = array( diff --git a/modules/simpletest/simpletest.info b/modules/simpletest/simpletest.info index 15d2bfd0e..26647b7a2 100644 --- a/modules/simpletest/simpletest.info +++ b/modules/simpletest/simpletest.info @@ -40,6 +40,7 @@ files[] = tests/xmlrpc.test files[] = tests/upgrade/upgrade.test files[] = tests/upgrade/upgrade.comment.test files[] = tests/upgrade/upgrade.filter.test +files[] = tests/upgrade/upgrade.forum.test files[] = tests/upgrade/upgrade.node.test files[] = tests/upgrade/upgrade.taxonomy.test files[] = tests/upgrade/upgrade.upload.test diff --git a/modules/simpletest/tests/upgrade/drupal-6.forum.database.php b/modules/simpletest/tests/upgrade/drupal-6.forum.database.php new file mode 100644 index 000000000..07dfcb341 --- /dev/null +++ b/modules/simpletest/tests/upgrade/drupal-6.forum.database.php @@ -0,0 +1,261 @@ +<?php +// $Id$ + +/** + * Database additions for forum tests. + */ + +db_create_table('forum', array( + 'fields' => array( + 'nid' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), + 'vid' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), + 'tid' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), + ), + 'indexes' => array( + 'nid' => array( + 'nid', + ), + 'tid' => array( + 'tid', + ), + ), + 'primary key' => array( + 'vid', + ), + 'module' => 'forum', + 'name' => 'forum', +)); +db_insert('forum')->fields(array( + 'nid', + 'vid', + 'tid', +)) +->values(array( + 'nid' => '51', + 'vid' => '61', + 'tid' => '81', +)) +->execute(); + +db_insert('node')->fields(array( + 'nid', + 'vid', + 'type', + 'language', + 'title', + 'uid', + 'status', + 'created', + 'changed', + 'comment', + 'promote', + 'moderate', + 'sticky', + 'tnid', + 'translate', +)) +->values(array( + 'nid' => '51', + 'vid' => '61', + 'type' => 'forum', + 'language' => '', + 'title' => 'Apples', + 'uid' => '1', + 'status' => '1', + 'created' => '1298363952', + 'changed' => '1298363952', + 'comment' => '2', + 'promote' => '0', + 'moderate' => '0', + 'sticky' => '0', + 'tnid' => '0', + 'translate' => '0', +)) +->execute(); + +db_insert('node_revisions')->fields(array( + 'nid', + 'vid', + 'uid', + 'title', + 'body', + 'teaser', + 'log', + 'timestamp', + 'format', +)) +->values(array( + 'nid' => '51', + 'vid' => '61', + 'uid' => '1', + 'title' => 'Apples', + 'body' => 'A fruit.', + 'teaser' => 'A fruit.', + 'log' => '', + 'timestamp' => '1298363952', + 'format' => '1', +)) +->execute(); + +db_insert('node_comment_statistics')->fields(array( + 'nid', + 'last_comment_timestamp', + 'last_comment_name', + 'last_comment_uid', + 'comment_count', +)) +->values(array( + 'nid' => '51', + 'last_comment_timestamp' => '1298363952', + 'last_comment_name' => NULL, + 'last_comment_uid' => '1', + 'comment_count' => '0', +)) +->execute(); + +db_insert('node_type')->fields(array( + 'type', + 'name', + 'module', + 'description', + 'help', + 'has_title', + 'title_label', + 'has_body', + 'body_label', + 'min_word_count', + 'custom', + 'modified', + 'locked', + 'orig_type', +)) +->values(array( + 'type' => 'forum', + 'name' => 'Forum topic', + 'module' => 'forum', + 'description' => 'A <em>forum topic</em> is the initial post to a new discussion thread within a forum.', + 'help' => '', + 'has_title' => '1', + 'title_label' => 'Subject', + 'has_body' => '1', + 'body_label' => 'Body', + 'min_word_count' => '0', + 'custom' => '0', + 'modified' => '0', + 'locked' => '1', + 'orig_type' => 'forum', +)) +->execute(); + +db_update('system')->fields(array( + 'schema_version' => '6000', + 'status' => '1', +)) +->condition('filename', 'modules/forum/forum.module') +->execute(); + +db_insert('term_data')->fields(array( + 'tid', + 'vid', + 'name', + 'description', + 'weight', +)) +->values(array( + 'tid' => '81', + 'vid' => '101', + 'name' => 'Fruits', + 'description' => 'Fruits.', + 'weight' => '0', +)) +->execute(); + +db_insert('term_hierarchy')->fields(array( + 'tid', + 'parent', +)) +->values(array( + 'tid' => '81', + 'parent' => '0', +)) +->execute(); + +db_insert('term_node')->fields(array( + 'nid', + 'vid', + 'tid', +)) +->values(array( + 'nid' => '51', + 'vid' => '61', + 'tid' => '81', +)) +->execute(); + +db_insert('variable')->fields(array( + 'name', + 'value', +)) +->values(array( + 'name' => 'forum_nav_vocabulary', + 'value' => 's:3:"101";', +)) +->values(array( + 'name' => 'forum_containers', + 'value' => 'a:1:{i:0;s:3:"101";}', +)) +->execute(); + +db_insert('vocabulary')->fields(array( + 'vid', + 'name', + 'description', + 'help', + 'relations', + 'hierarchy', + 'multiple', + 'required', + 'tags', + 'module', + 'weight', +)) +->values(array( + 'vid' => '101', + 'name' => 'Upgrade test for forums', + 'description' => 'Vocabulary used for Forums. The name is changed from the default "Forums" so that the upgrade path may be tested.', + 'help' => '', + 'relations' => '1', + 'hierarchy' => '1', + 'multiple' => '0', + 'required' => '0', + 'tags' => '0', + 'module' => 'forum', + 'weight' => '-10', +)) +->execute(); + +db_insert('vocabulary_node_types')->fields(array( + 'vid', + 'type', +)) +->values(array( + 'vid' => '101', + 'type' => 'forum', +)) +->execute(); + diff --git a/modules/simpletest/tests/upgrade/upgrade.forum.test b/modules/simpletest/tests/upgrade/upgrade.forum.test new file mode 100644 index 000000000..827988dab --- /dev/null +++ b/modules/simpletest/tests/upgrade/upgrade.forum.test @@ -0,0 +1,61 @@ +<?php +// $Id$ + +/** + * Upgrade test for forum.module. + */ +class ForumUpgradePathTestCase extends UpgradePathTestCase { + public static function getInfo() { + return array( + 'name' => 'Forum upgrade path', + 'description' => 'Upgrade path tests for the Forum module.', + 'group' => 'Upgrade path', + ); + } + + public function setUp() { + // Path to the database dump files. + $this->databaseDumpFiles = array( + drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.filled.database.php', + drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.forum.database.php', + ); + parent::setUp(); + + $this->uninstallModulesExcept(array('comment', 'forum', 'taxonomy')); + } + + /** + * Test a successful upgrade (no negotiation). + */ + public function testForumUpgrade() { + $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.')); + + // Work around http://drupal.org/node/931512 + $this->drupalPost('admin/structure/types/manage/forum/fields', array(), t('Save')); + + // The D6 database forum vocabulary contains the term "Fruits" with id 81. + $tid = 81; + $this->drupalGet("forum/$tid"); + + // There is one forum topic in Fruits, with the title "Apples". + $this->clickLink('Apples'); + $this->clickLink('Edit'); + + // Add a forum topic "Bananas" to the "Fruits" forum. + $edit = array( + 'title' => $title = 'Bananas', + 'body[' . LANGUAGE_NONE . '][0][value]' => $body = 'It is another fruit.', + ); + $this->drupalPost("node/add/forum/$tid", $edit, t('Save')); + $type = t('Forum topic'); + $this->assertRaw(t('@type %title has been created.', array('@type' => $type, '%title' => $title)), t('Forum topic was created')); + + // Retrieve node object, ensure that the topic was created and in the proper forum. + $node = $this->drupalGetNodeByTitle($title); + $this->assertTrue($node != NULL, t('Node @title was loaded', array('@title' => $title))); + $this->assertEqual($node->taxonomy_forums[LANGUAGE_NONE][0]['tid'], $tid, 'Saved forum topic was in the expected forum'); + + $this->drupalGet("forum/$tid"); + $this->assertText('Bananas'); + } +} |