Commit 8cb0a194 authored by webchick's avatar webchick

Issue #1817182 by andypost, alexpott, yched: Add upgrade path tests for...

Issue #1817182 by andypost, alexpott, yched: Add upgrade path tests for contact category config conversion.
parent 3bee3571
......@@ -1254,6 +1254,26 @@ function update_variables_to_config($config_name, array $variable_map) {
db_delete('variable')->condition('name', array_keys($variable_map), 'IN')->execute();
}
/**
* Adds entries in a configuration entity manifest file during updates.
*
* @param string $config_prefix
* The configuration entity prefix from the annotation.
* @param array $ids
* An array of configuration entities to add to the manifest.
*/
function update_config_manifest_add($config_prefix, array $ids) {
$manifest = config('manifest.' . $config_prefix);
foreach ($ids as $id) {
// Add record to manifest for each config entity.
$manifest->set($id, array('name' => $config_prefix . '.' . $id));
}
// Write manifest to disk.
$manifest->save();
}
/**
* Updates 7.x variables to state records.
*
......
......@@ -5,6 +5,8 @@
* Install, update and uninstall functions for the contact module.
*/
use Drupal\Component\Uuid\Uuid;
/**
* Implements hook_install().
*/
......@@ -51,10 +53,14 @@ function contact_update_8000() {
* @ingroup config_upgrade
*/
function contact_update_8001() {
$uuid = new Uuid();
$result = db_query('SELECT * FROM {contact}');
$ids = array();
foreach ($result as $category) {
// Take over the category's serial ID as new machine name.
$category->id = $category->cid;
// Save the id to add to the manifest file.
$ids[] = $category->id;
// Save default category setting.
if ($category->selected) {
config('contact.settings')
......@@ -64,12 +70,16 @@ function contact_update_8001() {
// Save the config object.
config('contact.category.' . $category->id)
->set('id', $category->id)
->set('uuid', $uuid->generate())
->set('label', $category->category)
->set('recipients', explode(',', $category->recipients))
->set('reply', $category->reply)
->set('weight', $category->weight)
->set('langcode', LANGUAGE_NOT_SPECIFIED)
->save();
}
update_config_manifest_add('contact.category', $ids);
}
/**
......
<?php
/**
* @file
* Definition of Drupal\contact\Tests\ContactUpgradePathTest.
*/
namespace Drupal\contact\Tests;
use Drupal\system\Tests\Upgrade\UpgradePathTestBase;
/**
* Tests upgrade of contact.
*/
class ContactUpgradePathTest extends UpgradePathTestBase {
public static function getInfo() {
return array(
'name' => 'Contact upgrade test',
'description' => 'Tests upgrade of contact to the configuration system.',
'group' => 'Contact',
);
}
public function setUp() {
$this->databaseDumpFiles = array(
drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.bare.standard_all.database.php.gz',
drupal_get_path('module', 'contact') . '/tests/drupal-7.contact.database.php',
);
parent::setUp();
}
/**
* Tests upgrade of contact table to configuration entities.
*/
public function testContactUpgrade() {
$default_contact_category = db_query('SELECT cid FROM {contact} WHERE selected = 1')->fetchField();
$this->assertTrue(db_table_exists('contact'), 'Contact table exists.');
$this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.');
// Test that the contact form is available.
$this->assertFalse(db_table_exists('contact'), 'Contact table has been deleted.');
$this->drupalGet('contact');
$this->assertText(t('Your e-mail address'));
// Ensure that the Drupal 7 default contact category has been created.
$contact_category = entity_load('contact_category', '1');
$this->assertTrue(isset($contact_category->uuid), 'Converted contact category has a UUID');
$this->assertEqual($contact_category->label, 'Website feedback');
$this->assertEqual($contact_category->reply, '');
$this->assertEqual($contact_category->weight, 0);
$this->assertEqual($contact_category->recipients, array('admin@example.com'));
// Test that the custom contact category has been updated.
$contact_category = entity_load('contact_category', '2');
$this->assertTrue(isset($contact_category->uuid), 'Converted contact category has a UUID');
$this->assertEqual($contact_category->label, 'Upgrade test');
$this->assertEqual($contact_category->reply, 'Test reply');
$this->assertEqual($contact_category->weight, 1);
$this->assertEqual($contact_category->recipients, array('test1@example.com', 'test2@example.com'));
// Ensure that the default category has been maintained.
$this->assertEqual(config('contact.settings')->get('default_category'), $default_contact_category, 'Default category upgraded.');
// Check that no default config imported on upgrade.
$this->assertFalse(entity_load('contact_category', 'feedback'));
// Assert that manifest has been created and contains the expected records.
$manifest = config('manifest.contact.category');
$this->assertEqual($manifest->get('1.name'), 'contact.category.1');
$this->assertEqual($manifest->get('2.name'), 'contact.category.2');
}
}
<?php
/**
* @file
* Database additions for Drupal\contact\Tests\ContactUpgradePathTest.
*
* This dump only contains data for the contact module. The
* drupal-7.filled.bare.php file is imported before this dump, so the two form
* the database structure expected in tests altogether.
*/
// Update the default category to that it is not selected.
db_update('contact')
->fields(array('selected' => '0'))
->condition('cid', '1')
->execute();
// Add a custom contact category.
db_insert('contact')->fields(array(
'category',
'recipients',
'reply',
'weight',
'selected'
))
->values(array(
'category' => 'Upgrade test',
'recipients'=> 'test1@example.com,test2@example.com',
'reply' => 'Test reply',
'weight' => 1,
'selected' => 1,
))
->execute();
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