Commit ec85f871 authored by Croitor Alexandru's avatar Croitor Alexandru
Browse files

Added a few simple SimpleTest tests. Added forum conversion possibility. Improved code.

parent 3ce68eb3
......@@ -61,6 +61,8 @@ function node_convert_conversion_form($form_state, $node) {
}
elseif ($form_state['values']['step'] == 'choose_destination_type') {
$op = 'choose_destination_fields';
} elseif (isset($form_state['values']['step'])) {
$op = $form_state['values']['step'];
}
$form['step'] = array(
'#type' => 'value',
......@@ -86,7 +88,7 @@ function node_convert_conversion_form($form_state, $node) {
if (count($source_fields) == 0) {
// In case there are no cck fields, just convert the node type
$form['no_fields_flag'] = array('#type' => 'value', '#value' => 'true');
$form['no_fields'] = array('#type' => 'markup', '#value' => t("There are no cck fields to convert. Please press submit."));
$form['no_fields'] = array('#type' => 'markup', '#value' => t("There are no cck fields to convert. Please press Convert."));
}
else { // Otherwise
foreach ($source_fields as $field) {
......@@ -135,11 +137,21 @@ function node_convert_conversion_form($form_state, $node) {
}
}
$form['submit'] = array('#type' => 'submit', '#value' => t("Submit"));
if ($op != 'choose_destination_fields') {
$form['submit'] = array('#type' => 'submit', '#value' => t("Next"));
} else {
$form['submit'] = array('#type' => 'submit', '#value' => t("Convert"));
}
return $form;
}
function node_convert_conversion_form_validate($form, &$form_state) {
if ($form_state['values']['step'] == 'choose_destination_fields') {
module_invoke_all('node_convert_change', array('dest_node_type' => $form_state['storage']['destination_type'], 'form_state' => $form_state), 'options validate');
}
}
function node_convert_conversion_form_submit($form, &$form_state) {
// Remember the destination type
if ($form_state['values']['step'] == 'choose_destination_type') {
......@@ -166,9 +178,9 @@ function node_convert_conversion_form_submit($form, &$form_state) {
} else {
$hook_options = NULL;
}
node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag, $hook_options);
drupal_set_message("The current node has been converted succesufuly");
$result = node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag, $hook_options);
// We display errors if any, or the default succesuful message
node_convert_messages($result, array('nid' => $nid));
// We clear the storage so redirect works
$form_state['storage'] = array();
$form_state['redirect'] = "node/". $nid;
......@@ -185,7 +197,6 @@ function theme_node_convert_conversion_form($form) {
}
function node_convert_bulk($form_state) {
//var_dump($form_state);
$form = array();
/* Setting the steps */
......@@ -199,7 +210,7 @@ function node_convert_bulk($form_state) {
$op = 'choose_fields';
}
$form['step'] = array(
'#type' => 'hidden',
'#type' => 'value',
'#value' => $op,
);
......@@ -266,7 +277,11 @@ function node_convert_bulk($form_state) {
$form['info'] = array('#type' => 'value', '#value' => $info);
}
$form['submit'] = array('#type' => 'submit', '#value' => t("Submit"));
if ($op != 'choose_fields') {
$form['submit'] = array('#type' => 'submit', '#value' => t("Next"));
} else {
$form['submit'] = array('#type' => 'submit', '#value' => t("Convert"));
}
return $form;
}
......@@ -288,8 +303,9 @@ function node_convert_bulk_validate($form, &$form_state) {
if ($no_nodes == TRUE) {
form_set_error('', t('Please select at least one node.'));
}
} elseif ($form_state['values']['step'] == 'choose_fields') {
module_invoke_all('node_convert_change', array('dest_node_type' => $form_state['values']['info']['dest_type'], 'form_state' => $form_state), 'options validate');
}
}
function node_convert_bulk_submit($form, &$form_state) {
......@@ -309,7 +325,9 @@ function node_convert_bulk_submit($form, &$form_state) {
$hook_options = NULL;
}
foreach ($info['nodes'] as $nid) {
node_convert_node_convert($nid, $info['dest_type'], $info['fields']['source'], $info['fields']['destination'], $info['no_fields'], $hook_options);
$result = node_convert_node_convert($nid, $info['dest_type'], $info['fields']['source'], $info['fields']['destination'], $info['no_fields'], $hook_options);
// We display errors if any, or the default succesuful message
node_convert_messages($result, array('nid' => $nid, 'display_success' => FALSE));
}
drupal_set_message("Nodes converted succesufuly.");
......@@ -351,7 +369,7 @@ function theme_node_convert_bulk($form) {
elseif ($form['step']['#value'] == "choose_fields") {
$output = '<div>'. t("Choose the fields where the values should be stored.") .'</div>';
$info = $form['info']['#value'];
if ($info['no_fields'] == TRUE) $output .= '<div>'. t("There are no cck fields to convert. Please press submit.") .'</div>';
if ($info['no_fields'] == TRUE) $output .= '<div>'. t("There are no cck fields to convert. Please press Convert.") .'</div>';
$output .= drupal_render($form);
}
return $output;
......@@ -369,9 +387,16 @@ function theme_node_convert_bulk($form) {
* An array containing the destination field names.
* @param $no_fields_flag
* A boolean containing if there are source fields that have to be converted.
* @param $hook_options
* An array containing values used by the hook_node_convert_change functions.
* @return
* Returns the $nid.
*/
function node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag, $hook_options = NULL) {
$node = node_load($nid);
if ($node == FALSE) {
return FALSE;
}
$vid = $node->vid;
$source_node_type = $node->type;
$tables_info = content_types();
......@@ -398,6 +423,7 @@ function node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_
db_query("DELETE FROM {cache_content} WHERE cid = '%s'", "content:". $nid .":". $vid); // We clear the cache
cache_clear_all('node:'. $nid, 'cache_menu', 'TRUE');
cache_clear_all('node/'. $nid, 'cache_menu', 'TRUE');
return $nid;
}
/**
......@@ -483,6 +509,27 @@ function node_convert_field_convert($nid, $source_field, $dest_field) {
}
}
/**
* Displays error messages if any occured, otherwise the success message.
*
* @param $result
* The result value of the node conversion. Possible values
* - FALSE Displays an error message.
* - Any other Displays success message.
* @param $params
* An array containing message parameters. Possible values
* - display_success If TRUE, the success message will be displayed, otherwise no message is displayed.
* Default is TRUE.
*/
function node_convert_messages($result, $params = array()) {
$params += array('display_success' => TRUE);
if ($result == FALSE) {
drupal_set_message(t("Conversion failed. Node nid @nid doesn't exist.", array('@nid' => $params['nid'])), 'error');
} elseif ($params['display_success'] == TRUE) {
drupal_set_message(t("The current node has been converted succesufuly."));
}
}
/**
* Preforms actions when converting a module based on the node type.
*
......@@ -490,6 +537,7 @@ function node_convert_field_convert($nid, $source_field, $dest_field) {
* An array containing information about the conversion process. The keys are
* - dest_node_type The destination type of the node
* - node The node object
* - Any other information passed by $op = 'options' or $op = 'options validate'
* @param $op
* A string containg the operation which should be executed. These are the possible values
* - insert Operations which should be run when the node is transferred to the new node type.
......@@ -497,6 +545,7 @@ function node_convert_field_convert($nid, $source_field, $dest_field) {
* - delete Operations which should be run after the node is transferred to the new node type.
* Usually for deleting unneeded information from the database after the transfer.
* - options Configuration elements shown on the conversion form. Should return a FAPI array.
* - options validate Validation check on the options elements.
* @return
* Should return a FAPI array only when using the options operation.
*/
......@@ -514,11 +563,19 @@ function node_convert_node_convert_change($data, $op) {
if ($book['bid'] == 'self') $book['bid'] = $node->nid;
db_query("INSERT INTO {book} (nid, mlid, bid) VALUES (%d, %d, %d)", $node->nid, $book['mlid'], $book['bid']);
}
if ($data['dest_node_type'] == 'forum') {
db_query('INSERT INTO {forum} (tid, vid, nid) VALUES (%d, %d, %d)', $data['hook_options']['forum'], $data['node']->vid, $data['node']->nid);
db_query('INSERT INTO {term_node} (tid, vid, nid) VALUES (%d, %d, %d)', $data['hook_options']['forum'], $data['node']->vid, $data['node']->nid);
}
} elseif ($op == 'delete') {
if ($data['node']->type == 'book') {
menu_link_delete($data['node']->book['mlid']);
db_query('DELETE FROM {book} WHERE mlid = %d', $data['node']->book['mlid']);
}
if ($data['node']->type == 'forum') {
db_query('DELETE FROM {forum} WHERE nid = %d', $data['node']->nid);
db_query('DELETE FROM {term_node} WHERE nid = %d', $data['node']->nid);
}
} elseif ($op == 'options') {
$form = array();
if ($data['dest_node_type'] == 'book') {
......@@ -533,7 +590,24 @@ function node_convert_node_convert_change($data, $op) {
'#description' => t('Your page will be a part of the selected book.'),
'#attributes' => array('class' => 'book-title-select'),
);
}
}
if ($data['dest_node_type'] == 'forum') {
$vid = variable_get('forum_nav_vocabulary', '');
$form['forum'] = taxonomy_form($vid);
$form['forum']['#weight'] = 7;
$form['forum']['#required'] = TRUE;
$form['forum']['#options'][''] = t('- Please choose -');
}
return $form;
} elseif ($op == 'options validate') {
$form_state = $data['form_state'];
if ($data['dest_node_type'] == 'forum') {
$containers = variable_get('forum_containers', array());
$term = $form_state['values']['hook_options']['forum'];
if (in_array($term, $containers)) {
$term = taxonomy_get_term($term);
form_set_error('hook_options][forum', t('The item %forum is only a container for forums. Please select one of the forums below it.', array('%forum' => $term->name)));
}
}
}
}
\ No newline at end of file
<?php
// $Id$
class NodeConvertTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Node conversion'),
'description' => t('Creating a node, converting it to another type, delete it.'),
'group' => t('NodeConvert'),
);
}
function setUp() {
// The first thing a setUp() method should always do is call its parent setUp() method.
// If you need to enable any modules (including the one being tested),
// add them as function parameters.
parent::setUp('content', 'node_convert');
// Next, perform any required steps for the test methods within this test grouping.
}
/**
* Implementation of tearDown().
*/
function tearDown() {
// Perform any clean-up tasks.
// The last thing a tearDown() method should always do is call its parent tearDown() method.
parent::tearDown();
}
function testSimpleNodeConvert() {
$type1_name = $this->randomName(4);
$type2_name = $this->randomName(4);
$type1 = $this->drupalCreateContentType(array('type' => $type1_name, 'name' => $type1_name));
$type2 = $this->drupalCreateContentType(array('type' => $type2_name, 'name' => $type2_name));
$edit['type'] = $type1_name;
$node = $this->drupalCreateNode($edit);
node_convert_node_convert($node->nid, $type2_name, array(), array(), TRUE);
$result = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $node->nid));
$this->assertEqual($result, $type2_name, t("Simple node conversion passed."));
}
function testInvalidNidConvert() {
$result = node_convert_node_convert(-1, $this->randomName(4), array(), array(), TRUE);
$this->assertFalse($result, t("Node conversion didn't pass due to illegal nid."));
}
function testSimpleNodeConvertUI() {
$admin_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages', 'administer nodes', 'administer content types'));
$this->drupalLogin($admin_user);
$type1_name = $this->randomName(4);
$type2_name = $this->randomName(4);
$type1 = $this->drupalCreateContentType(array('type' => $type1_name, 'name' => $type1_name));
$type2 = $this->drupalCreateContentType(array('type' => $type2_name, 'name' => $type2_name));
$edit['type'] = $type1_name;
$node = $this->drupalCreateNode($edit);
$edit = array();
$edit['destination_type'] = $type2_name;
$this->drupalPost('node/'. $node->nid .'/convert', $edit, t("Next"));
$this->drupalPost(NULL, array(), t("Convert"));
$this->assertText(t("The current node has been converted succesufuly."), t("Simple node conversion ui test passed."));
}
}
\ No newline at end of file
Supports Markdown
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