generate-d7-content.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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' => 'select',
  123. 'module' => 'options',
  124. 'settings' => array(),
  125. );
  126. }
  127. field_create_instance($instance);
  128. }
  129. $parents = array();
  130. // Vocabularies without hierarchy get one term; single parent vocabularies
  131. // get one parent and one child term. Multiple parent vocabularies get
  132. // three terms: t0, t1, t2 where t0 is a parent of both t1 and t2.
  133. for ($j = 0; $j < $vocabulary->hierarchy + 1; $j++) {
  134. $term = new stdClass;
  135. $term->vocabulary_machine_name = $vocabulary->machine_name;
  136. // For multiple parent vocabularies, omit the t0-t1 relation, otherwise
  137. // every parent in the vocabulary is a parent.
  138. $term->parent = $vocabulary->hierarchy == 2 && i == 1 ? array() : $parents;
  139. ++$term_id;
  140. $term->name = "term $term_id of vocabulary $voc_id (j=$j)";
  141. $term->description = 'description of ' . $term->name;
  142. $term->format = 'filtered_html';
  143. $term->weight = $i * 3 + $j;
  144. taxonomy_term_save($term);
  145. $terms[] = $term->tid;
  146. $term_vocabs[$term->tid] = 'taxonomy_' . $vocabulary->machine_name;
  147. $parents[] = $term->tid;
  148. }
  149. }
  150. }
  151. $node_id = 0;
  152. $revision_id = 0;
  153. module_load_include('inc', 'node', 'node.pages');
  154. for ($i = 0; $i < 24; $i++) {
  155. $uid = intval($i / 8) + 3;
  156. $user = user_load($uid);
  157. $node = new stdClass();
  158. $node->uid = $uid;
  159. $node->type = $i < 12 ? 'page' : 'story';
  160. $node->sticky = 0;
  161. ++$node_id;
  162. ++$revision_id;
  163. $node->title = "node title $node_id rev $revision_id (i=$i)";
  164. $node->language = LANGUAGE_NONE;
  165. $body_text = str_repeat("node body ($node->type) - $i", 100);
  166. $node->body[$node->language][0]['value'] = $body_text;
  167. $node->body[$node->language][0]['summary'] = text_summary($body_text);
  168. $node->body[$node->language][0]['format'] = 'filtered_html';
  169. $node->status = intval($i / 4) % 2;
  170. $node->revision = $i < 12;
  171. $node->promote = $i % 2;
  172. $node->created = $now + $i * 86400;
  173. $node->log = "added $i node";
  174. // Make every term association different a little. For nodes with revisions,
  175. // make the initial revision have a different set of terms than the
  176. // newest revision.
  177. $items = array();
  178. if (module_exists('taxonomy')) {
  179. if ($node->revision) {
  180. $node_terms = array($terms[$i], $terms[47-$i]);
  181. }
  182. else {
  183. $node_terms = $terms;
  184. unset($node_terms[$i], $node_terms[47 - $i]);
  185. }
  186. foreach ($node_terms as $tid) {
  187. $field_name = $term_vocabs[$tid];
  188. $node->{$field_name}[LANGUAGE_NONE][] = array('tid' => $tid);
  189. }
  190. }
  191. $node->path = array('alias' => "content/$node->created");
  192. node_save($node);
  193. if ($node->revision) {
  194. $user = user_load($uid + 3);
  195. ++$revision_id;
  196. $node->title .= " rev2 $revision_id";
  197. $body_text = str_repeat("node revision body ($node->type) - $i", 100);
  198. $node->body[$node->language][0]['value'] = $body_text;
  199. $node->body[$node->language][0]['summary'] = text_summary($body_text);
  200. $node->body[$node->language][0]['format'] = 'filtered_html';
  201. $node->log = "added $i revision";
  202. $node_terms = $terms;
  203. unset($node_terms[$i], $node_terms[47 - $i]);
  204. foreach ($node_terms as $tid) {
  205. $field_name = $term_vocabs[$tid];
  206. $node->{$field_name}[LANGUAGE_NONE][] = array('tid' => $tid);
  207. }
  208. node_save($node);
  209. }
  210. }
  211. if (module_exists('poll')) {
  212. // Create poll content.
  213. for ($i = 0; $i < 12; $i++) {
  214. $uid = intval($i / 4) + 3;
  215. $user = user_load($uid);
  216. $node = new stdClass();
  217. $node->uid = $uid;
  218. $node->type = 'poll';
  219. $node->sticky = 0;
  220. $node->title = "poll title $i";
  221. $node->language = LANGUAGE_NONE;
  222. $node->status = intval($i / 2) % 2;
  223. $node->revision = 1;
  224. $node->promote = $i % 2;
  225. $node->created = REQUEST_TIME + $i * 43200;
  226. $node->runtime = 0;
  227. $node->active = 1;
  228. $node->log = "added $i poll";
  229. $node->path = array('alias' => "content/poll/$i");
  230. $nbchoices = ($i % 4) + 2;
  231. for ($c = 0; $c < $nbchoices; $c++) {
  232. $node->choice[] = array('chtext' => "Choice $c for poll $i", 'chvotes' => 0, 'weight' => 0);
  233. }
  234. node_save($node);
  235. $path = array(
  236. 'alias' => "content/poll/$i/results",
  237. 'source' => "node/$node->nid/results",
  238. );
  239. path_save($path);
  240. // Add some votes.
  241. $node = node_load($node->nid);
  242. $choices = array_keys($node->choice);
  243. $original_user = $GLOBALS['user'];
  244. for ($v = 0; $v < ($i % 4); $v++) {
  245. drupal_static_reset('ip_address');
  246. $_SERVER['REMOTE_ADDR'] = "127.0.$v.1";
  247. $GLOBALS['user'] = drupal_anonymous_user();// We should have already allowed anon to vote.
  248. $c = $v % $nbchoices;
  249. $form_state = array();
  250. $form_state['values']['choice'] = $choices[$c];
  251. $form_state['values']['op'] = t('Vote');
  252. drupal_form_submit('poll_view_voting', $form_state, $node);
  253. }
  254. }
  255. }
  256. // Test that upgrade works even on a bundle whose parent module was disabled.
  257. // This is simulated by creating an existing content type and changing the
  258. // bundle to another type through direct database update queries.
  259. $node_type = 'broken';
  260. $uid = 6;
  261. $user = user_load($uid);
  262. $node = new stdClass();
  263. $node->uid = $uid;
  264. $node->type = 'article';
  265. $body_text = str_repeat("node body ($node_type) - 37", 100);
  266. $node->sticky = 0;
  267. $node->title = "node title 24";
  268. $node->language = LANGUAGE_NONE;
  269. $node->body[$node->language][0]['value'] = $body_text;
  270. $node->body[$node->language][0]['summary'] = text_summary($body_text);
  271. $node->body[$node->language][0]['format'] = 'filtered_html';
  272. $node->status = 1;
  273. $node->revision = 0;
  274. $node->promote = 0;
  275. $node->created = 1263769200;
  276. $node->log = "added a broken node";
  277. $node->path = array('alias' => "content/1263769200");
  278. node_save($node);
  279. db_update('node')
  280. ->fields(array(
  281. 'type' => $node_type,
  282. ))
  283. ->condition('nid', $node->nid)
  284. ->execute();
  285. if (db_table_exists('field_data_body')) {
  286. db_update('field_data_body')
  287. ->fields(array(
  288. 'bundle' => $node_type,
  289. ))
  290. ->condition('entity_id', $node->nid)
  291. ->condition('entity_type', 'node')
  292. ->execute();
  293. db_update('field_revision_body')
  294. ->fields(array(
  295. 'bundle' => $node_type,
  296. ))
  297. ->condition('entity_id', $node->nid)
  298. ->condition('entity_type', 'node')
  299. ->execute();
  300. }
  301. db_update('field_config_instance')
  302. ->fields(array(
  303. 'bundle' => $node_type,
  304. ))
  305. ->condition('bundle', 'article')
  306. ->execute();