Generates content for a Drupal 7 database to test the upgrade process.

Run this script at the root of an existing Drupal 6 installation. Steps to use this generation script:

  • Install drupal 7.
  • Run this script from your Drupal ROOT directory.
  • Use the dump-database-d7.sh to generate the D7 file modules/simpletest/tests/upgrade/database.filled.php

File

scripts/generate-d7-content.sh
View source
  1. #!/usr/bin/env php
  2. /**
  3. * @file
  4. * Generates content for a Drupal 7 database to test the upgrade process.
  5. *
  6. * Run this script at the root of an existing Drupal 6 installation.
  7. * Steps to use this generation script:
  8. * - Install drupal 7.
  9. * - Run this script from your Drupal ROOT directory.
  10. * - Use the dump-database-d7.sh to generate the D7 file
  11. * modules/simpletest/tests/upgrade/database.filled.php
  12. */
  13. // Define settings.
  14. $cmd = 'index.php';
  15. define('DRUPAL_ROOT', getcwd());
  16. $_SERVER['HTTP_HOST'] = 'default';
  17. $_SERVER['PHP_SELF'] = '/index.php';
  18. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  19. $_SERVER['SERVER_SOFTWARE'] = NULL;
  20. $_SERVER['REQUEST_METHOD'] = 'GET';
  21. $_SERVER['QUERY_STRING'] = '';
  22. $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/';
  23. $_SERVER['HTTP_USER_AGENT'] = 'console';
  24. $modules_to_enable = array('path', 'poll', 'taxonomy');
  25. // Bootstrap Drupal.
  26. include_once './includes/bootstrap.inc';
  27. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  28. // Enable requested modules.
  29. require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
  30. include_once './modules/system/system.admin.inc';
  31. $form = system_modules();
  32. foreach ($modules_to_enable as $module) {
  33. $form_state['values']['status'][$module] = TRUE;
  34. }
  35. $form_state['values']['disabled_modules'] = $form['disabled_modules'];
  36. system_modules_submit(NULL, $form_state);
  37. unset($form_state);
  38. // Run cron after installing.
  39. drupal_cron_run();
  40. // Create six users.
  41. $query = db_insert('users')->fields(array('uid', 'name', 'pass', 'mail', 'status', 'created', 'access'));
  42. for ($i = 0; $i < 6; $i++) {
  43. $name = "test user $i";
  44. $pass = md5("test PassW0rd $i !(.)");
  45. $mail = "test$i@example.com";
  46. $now = mktime(0, 0, 0, 1, $i + 1, 2010);
  47. $query->values(array(db_next_id(), $name, user_hash_password($pass), $mail, 1, $now, $now));
  48. }
  49. $query->execute();
  50. // Create vocabularies and terms.
  51. if (module_exists('taxonomy')) {
  52. $terms = array();
  53. // All possible combinations of these vocabulary properties.
  54. $hierarchy = array(0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2);
  55. $multiple = array(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1);
  56. $required = array(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1);
  57. $voc_id = 0;
  58. $term_id = 0;
  59. for ($i = 0; $i < 24; $i++) {
  60. $vocabulary = new stdClass;
  61. ++$voc_id;
  62. $vocabulary->name = "vocabulary $voc_id (i=$i)";
  63. $vocabulary->machine_name = 'vocabulary_' . $voc_id . '_' . $i;
  64. $vocabulary->description = "description of ". $vocabulary->name;
  65. $vocabulary->multiple = $multiple[$i % 12];
  66. $vocabulary->required = $required[$i % 12];
  67. $vocabulary->relations = 1;
  68. $vocabulary->hierarchy = $hierarchy[$i % 12];
  69. $vocabulary->weight = $i;
  70. taxonomy_vocabulary_save($vocabulary);
  71. $field = array(
  72. 'field_name' => 'taxonomy_'. $vocabulary->machine_name,
  73. 'module' => 'taxonomy',
  74. 'type' => 'taxonomy_term_reference',
  75. 'cardinality' => $vocabulary->multiple || $vocabulary->tags ? FIELD_CARDINALITY_UNLIMITED : 1,
  76. 'settings' => array(
  77. 'required' => $vocabulary->required ? TRUE : FALSE,
  78. 'allowed_values' => array(
  79. array(
  80. 'vocabulary' => $vocabulary->machine_name,
  81. 'parent' => 0,
  82. ),
  83. ),
  84. ),
  85. );
  86. field_create_field($field);
  87. $node_types = $i > 11 ? array('page') : array_keys(node_type_get_types());
  88. foreach ($node_types as $bundle) {
  89. $instance = array(
  90. 'label' => $vocabulary->name,
  91. 'field_name' => $field['field_name'],
  92. 'bundle' => $bundle,
  93. 'entity_type' => 'node',
  94. 'settings' => array(),
  95. 'description' => $vocabulary->help,
  96. 'required' => $vocabulary->required,
  97. 'widget' => array(),
  98. 'display' => array(
  99. 'default' => array(
  100. 'type' => 'taxonomy_term_reference_link',
  101. 'weight' => 10,
  102. ),
  103. 'teaser' => array(
  104. 'type' => 'taxonomy_term_reference_link',
  105. 'weight' => 10,
  106. ),
  107. ),
  108. );
  109. if ($vocabulary->tags) {
  110. $instance['widget'] = array(
  111. 'type' => 'taxonomy_autocomplete',
  112. 'module' => 'taxonomy',
  113. 'settings' => array(
  114. 'size' => 60,
  115. 'autocomplete_path' => 'taxonomy/autocomplete',
  116. ),
  117. );
  118. }
  119. else {
  120. $instance['widget'] = array(
  121. 'type' => 'options_select',
  122. 'settings' => array(),
  123. );
  124. }
  125. field_create_instance($instance);
  126. }
  127. $parents = array();
  128. // Vocabularies without hierarchy get one term; single parent vocabularies
  129. // get one parent and one child term. Multiple parent vocabularies get
  130. // three terms: t0, t1, t2 where t0 is a parent of both t1 and t2.
  131. for ($j = 0; $j < $vocabulary->hierarchy + 1; $j++) {
  132. $term = new stdClass;
  133. $term->vocabulary_machine_name = $vocabulary->machine_name;
  134. // For multiple parent vocabularies, omit the t0-t1 relation, otherwise
  135. // every parent in the vocabulary is a parent.
  136. $term->parent = $vocabulary->hierarchy == 2 && i == 1 ? array() : $parents;
  137. ++$term_id;
  138. $term->name = "term $term_id of vocabulary $voc_id (j=$j)";
  139. $term->description = 'description of ' . $term->name;
  140. $term->format = 'filtered_html';
  141. $term->weight = $i * 3 + $j;
  142. taxonomy_term_save($term);
  143. $terms[] = $term->tid;
  144. $term_vocabs[$term->tid] = 'taxonomy_' . $vocabulary->machine_name;
  145. $parents[] = $term->tid;
  146. }
  147. }
  148. }
  149. $node_id = 0;
  150. $revision_id = 0;
  151. module_load_include('inc', 'node', 'node.pages');
  152. for ($i = 0; $i < 24; $i++) {
  153. $uid = intval($i / 8) + 3;
  154. $user = user_load($uid);
  155. $node = new stdClass();
  156. $node->uid = $uid;
  157. $node->type = $i < 12 ? 'page' : 'story';
  158. $node->sticky = 0;
  159. ++$node_id;
  160. ++$revision_id;
  161. $node->title = "node title $node_id rev $revision_id (i=$i)";
  162. $node->language = LANGUAGE_NONE;
  163. $body_text = str_repeat("node body ($node->type) - $i", 100);
  164. $node->body[$node->language][0]['value'] = $body_text;
  165. $node->body[$node->language][0]['summary'] = text_summary($body_text);
  166. $node->body[$node->language][0]['format'] = 'filtered_html';
  167. $node->status = intval($i / 4) % 2;
  168. $node->revision = $i < 12;
  169. $node->promote = $i % 2;
  170. $node->created = $now + $i * 86400;
  171. $node->log = "added $i node";
  172. // Make every term association different a little. For nodes with revisions,
  173. // make the initial revision have a different set of terms than the
  174. // newest revision.
  175. $items = array();
  176. if (module_exists('taxonomy')) {
  177. if ($node->revision) {
  178. $node_terms = array($terms[$i], $terms[47-$i]);
  179. }
  180. else {
  181. $node_terms = $terms;
  182. unset($node_terms[$i], $node_terms[47 - $i]);
  183. }
  184. foreach ($node_terms as $tid) {
  185. $field_name = $term_vocabs[$tid];
  186. $node->{$field_name}[LANGUAGE_NONE][] = array('tid' => $tid);
  187. }
  188. }
  189. $node->path = array('alias' => "content/$node->created");
  190. node_save($node);
  191. if ($node->revision) {
  192. $user = user_load($uid + 3);
  193. ++$revision_id;
  194. $node->title .= " rev2 $revision_id";
  195. $body_text = str_repeat("node revision body ($node->type) - $i", 100);
  196. $node->body[$node->language][0]['value'] = $body_text;
  197. $node->body[$node->language][0]['summary'] = text_summary($body_text);
  198. $node->body[$node->language][0]['format'] = 'filtered_html';
  199. $node->log = "added $i revision";
  200. $node_terms = $terms;
  201. unset($node_terms[$i], $node_terms[47 - $i]);
  202. foreach ($node_terms as $tid) {
  203. $field_name = $term_vocabs[$tid];
  204. $node->{$field_name}[LANGUAGE_NONE][] = array('tid' => $tid);
  205. }
  206. node_save($node);
  207. }
  208. }
  209. if (module_exists('poll')) {
  210. // Create poll content.
  211. for ($i = 0; $i < 12; $i++) {
  212. $uid = intval($i / 4) + 3;
  213. $user = user_load($uid);
  214. $node = new stdClass();
  215. $node->uid = $uid;
  216. $node->type = 'poll';
  217. $node->sticky = 0;
  218. $node->title = "poll title $i";
  219. $node->language = LANGUAGE_NONE;
  220. $node->status = intval($i / 2) % 2;
  221. $node->revision = 1;
  222. $node->promote = $i % 2;
  223. $node->created = REQUEST_TIME + $i * 43200;
  224. $node->runtime = 0;
  225. $node->active = 1;
  226. $node->log = "added $i poll";
  227. $node->path = array('alias' => "content/poll/$i");
  228. $nbchoices = ($i % 4) + 2;
  229. for ($c = 0; $c < $nbchoices; $c++) {
  230. $node->choice[] = array('chtext' => "Choice $c for poll $i", 'chvotes' => 0, 'weight' => 0);
  231. }
  232. node_save($node);
  233. $path = array(
  234. 'alias' => "content/poll/$i/results",
  235. 'source' => "node/$node->nid/results",
  236. );
  237. path_save($path);
  238. // Add some votes.
  239. $node = node_load($node->nid);
  240. $choices = array_keys($node->choice);
  241. $original_user = $GLOBALS['user'];
  242. for ($v = 0; $v < ($i % 4); $v++) {
  243. drupal_static_reset('ip_address');
  244. $_SERVER['REMOTE_ADDR'] = "127.0.$v.1";
  245. $GLOBALS['user'] = drupal_anonymous_user();// We should have already allowed anon to vote.
  246. $c = $v % $nbchoices;
  247. $form_state = array();
  248. $form_state['values']['choice'] = $choices[$c];
  249. $form_state['values']['op'] = t('Vote');
  250. drupal_form_submit('poll_view_voting', $form_state, $node);
  251. }
  252. }
  253. }
  254. // Test that upgrade works even on a bundle whose parent module was disabled.
  255. // This is simulated by creating an existing content type and changing the
  256. // bundle to another type through direct database update queries.
  257. $node_type = 'broken';
  258. $uid = 6;
  259. $user = user_load($uid);
  260. $node = new stdClass();
  261. $node->uid = $uid;
  262. $node->type = 'article';
  263. $body_text = str_repeat("node body ($node_type) - 37", 100);
  264. $node->sticky = 0;
  265. $node->title = "node title 24";
  266. $node->language = LANGUAGE_NONE;
  267. $node->body[$node->language][0]['value'] = $body_text;
  268. $node->body[$node->language][0]['summary'] = text_summary($body_text);
  269. $node->body[$node->language][0]['format'] = 'filtered_html';
  270. $node->status = 1;
  271. $node->revision = 0;
  272. $node->promote = 0;
  273. $node->created = 1263769200;
  274. $node->log = "added a broken node";
  275. $node->path = array('alias' => "content/1263769200");
  276. node_save($node);
  277. db_update('node')
  278. ->fields(array(
  279. 'type' => $node_type,
  280. ))
  281. ->condition('nid', $node->nid)
  282. ->execute();
  283. if (db_table_exists('field_data_body')) {
  284. db_update('field_data_body')
  285. ->fields(array(
  286. 'bundle' => $node_type,
  287. ))
  288. ->condition('entity_id', $node->nid)
  289. ->condition('entity_type', 'node')
  290. ->execute();
  291. db_update('field_revision_body')
  292. ->fields(array(
  293. 'bundle' => $node_type,
  294. ))
  295. ->condition('entity_id', $node->nid)
  296. ->condition('entity_type', 'node')
  297. ->execute();
  298. }
  299. db_update('field_config_instance')
  300. ->fields(array(
  301. 'bundle' => $node_type,
  302. ))
  303. ->condition('bundle', 'article')
  304. ->execute();