generate-d7-content.sh 11 KB

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