PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/node_import/tests/NodeImportTestCase.php

https://github.com/arielalvarez88/yaquemotors
PHP | 284 lines | 129 code | 31 blank | 124 comment | 16 complexity | fb657638be6d06515e28d9cd2df5a876 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * DrupalWebTestCase wrapper with some extra functions to
  5. * ease the development of node_import tests.
  6. */
  7. class NodeImportTestCase extends DrupalWebTestCase {
  8. public function setUp() {
  9. $modules = func_get_args();
  10. $modules[] = 'node_import';
  11. $modules[] = 'date_api';
  12. call_user_func_array(array('parent', 'setUp'), $modules);
  13. }
  14. public function tearDown() {
  15. parent::tearDown();
  16. }
  17. /**
  18. * Output variable in test results.
  19. *
  20. * @param ...
  21. * Variables to dump.
  22. *
  23. * @return
  24. * Nothing.
  25. */
  26. public function debug() {
  27. $vars = func_get_args();
  28. foreach ($vars as $var) {
  29. $this->fail("<pre>" . print_r($var, TRUE) . "</pre>", 'Debug');
  30. }
  31. }
  32. /**
  33. * Create the user that will do the import.
  34. *
  35. * @param $perms
  36. * Array of permissions. The 'import content' permission
  37. * will be added.
  38. *
  39. * @return
  40. * User object.
  41. */
  42. public function nodeImportCreateUser($perms = array()) {
  43. if (!is_array($perms)) {
  44. $perms = func_get_args();
  45. }
  46. $perms[] = 'import content';
  47. $user = $this->drupalCreateUser($perms);
  48. return $user;
  49. }
  50. /**
  51. * Create a file with the specified data.
  52. *
  53. * @param $data
  54. * Twodimensional array.
  55. *
  56. * @param $file_options
  57. * Array with record separator, etc.
  58. *
  59. * @return
  60. * String. Path to file (in temporary directory).
  61. */
  62. public function nodeImportCreateFile($data, $file_options = array()) {
  63. module_load_include('inc', 'node_import');
  64. $path = file_create_filename($this->randomName() . '.csv', file_directory_temp());
  65. $fp = fopen($path, 'w');
  66. foreach ($data as $row) {
  67. $s = node_import_write_to_string($row, $file_options);
  68. fputs($fp, $s);
  69. }
  70. fclose($fp);
  71. return $path;
  72. }
  73. /**
  74. * Create an import task by going through the form.
  75. *
  76. * Note that the user needs to be logged in. Also note that this
  77. * will not start the import. You will need to run
  78. * $this->nodeImportDoAllTasks() for this which will try to finish
  79. * all import tasks.
  80. *
  81. * $data['file'] needs to be a full real path to a file.
  82. *
  83. * @param $data
  84. * Array of data to submit.
  85. *
  86. * @return
  87. * Import task id or FALSE if it fails.
  88. */
  89. public function nodeImportCreateTask($data) {
  90. $defaults = array(
  91. 'type' => '',
  92. 'file' => '',
  93. 'file_options' => array(),
  94. 'map' => array(),
  95. 'options' => array(),
  96. 'defaults' => array(),
  97. );
  98. $data = array_merge($defaults, $data);
  99. $step = 1;
  100. $steps = 8;
  101. $this->drupalPost('admin/content/node_import/add', array(), t('Next'));
  102. $title = t('Select content type');
  103. $expect = t('@title (step @step of @steps)', array('@title' => $title, '@step' => $step, '@steps' => $steps));
  104. if (!$this->assertRaw($expect, t('%title found.', array('%title' => $title)), 'Node import')) return FALSE;
  105. $step++;
  106. $this->drupalPost(NULL, array('type' => $data['type']), t('Next'));
  107. $title = t('Select file');
  108. $expect = t('@title (step @step of @steps)', array('@title' => $title, '@step' => $step, '@steps' => $steps));
  109. if (!$this->assertRaw($expect, t('%title found.', array('%title' => $title)), 'Node import')) return FALSE;
  110. $step++;
  111. $this->drupalPost(NULL, array('files[file_upload]' => realpath($data['file'])), t('Upload file'));
  112. $title = t('New file uploaded');
  113. $expect = t('New file %name uploaded to %path.', array('%name' => basename($data['file']), '%path' => node_import_directory() . basename($data['file'])));
  114. if (!$this->assertRaw($expect, t('%title found.', array('%title' => $title)), 'Node import')) return FALSE;
  115. $this->drupalPost(NULL, array(), t('Next'));
  116. $title = t('Set file options');
  117. $expect = t('@title (step @step of @steps)', array('@title' => $title, '@step' => $step, '@steps' => $steps));
  118. if (!$this->assertRaw($expect, t('%title found.', array('%title' => $title)), 'Node import')) return FALSE;
  119. $step++;
  120. $this->drupalPost(NULL, $data['file_options'], t('Next'));
  121. $title = t('Map file columns');
  122. $expect = t('@title (step @step of @steps)', array('@title' => $title, '@step' => $step, '@steps' => $steps));
  123. if (!$this->assertRaw($expect, t('%title found.', array('%title' => $title)), 'Node import')) return FALSE;
  124. $step++;
  125. $this->drupalPost(NULL, $data['map'], t('Next'));
  126. $title = t('Set import options');
  127. $expect = t('@title (step @step of @steps)', array('@title' => $title, '@step' => $step, '@steps' => $steps));
  128. if (!$this->assertRaw($expect, t('%title found.', array('%title' => $title)), 'Node import')) return FALSE;
  129. $step++;
  130. $this->drupalPost(NULL, $data['options'], t('Next'));
  131. $title = t('Set default values');
  132. $expect = t('@title (step @step of @steps)', array('@title' => $title, '@step' => $step, '@steps' => $steps));
  133. if (!$this->assertRaw($expect, t('%title found.', array('%title' => $title)), 'Node import')) return FALSE;
  134. $step++;
  135. $this->drupalPost(NULL, $data['defaults'], t('Next'));
  136. $title = t('Preview import');
  137. $expect = t('@title (step @step of @steps)', array('@title' => $title, '@step' => $step, '@steps' => $steps));
  138. if (!$this->assertRaw($expect, t('%title found.', array('%title' => $title)), 'Node import')) return FALSE;
  139. $step++;
  140. $this->drupalPost(NULL, array(), t('Next'));
  141. $title = t('Start import');
  142. $expect = t('@title (step @step of @steps)', array('@title' => $title, '@step' => $step, '@steps' => $steps));
  143. if (!$this->assertRaw($expect, t('%title found.', array('%title' => $title)), 'Node import')) return FALSE;
  144. $step++;
  145. $this->drupalPost(NULL, array(), t('Start import'));
  146. $url = url('admin/content/node_import/', array('absolute' => TRUE));
  147. if (!$this->assertTrue(strpos($this->getUrl(), $url) === 0, t('Redirected to import progress page.'), 'Node import')) return FALSE;
  148. $taskid = substr($this->getUrl(), strlen($url));
  149. $this->pass(t('Import task %taskid created.', array('%taskid' => $taskid)), 'Node import');
  150. return $taskid;
  151. }
  152. /**
  153. * Finish all pending import tasks.
  154. *
  155. * @param $max_runs
  156. * Integer. Maximum number of iterations.
  157. *
  158. * @return
  159. * Number of cron runs needed.
  160. */
  161. public function nodeImportDoAllTasks($max_runs = 100) {
  162. module_load_include('inc', 'node_import');
  163. $runs = 0;
  164. while ($runs <= $max_runs) {
  165. $runs++;
  166. node_import_do_all_tasks('ms', '1000');
  167. $result = db_result(db_query("SELECT count(*) FROM {node_import_tasks} WHERE status <> %d", NODE_IMPORT_STATUS_DONE));
  168. if ($result > 0) continue;
  169. break;
  170. }
  171. $this->assertTrue($runs <= $max_runs, t('Needed %runs iterations (of %max_runs allowed).', array('%runs' => $runs, '%max_runs' => $max_runs)), 'Node import');
  172. return $runs;
  173. }
  174. /**
  175. * Check to see if the row status is as specified.
  176. *
  177. * @param $result
  178. * Record from {node_import_status}.
  179. *
  180. * @param $status
  181. * What the status should be.
  182. *
  183. * @param $message
  184. * Message to display along with the assertion.
  185. *
  186. * @param $group
  187. * The type of assertion - examples are "Browser", "PHP".
  188. *
  189. * @return
  190. * TRUE if the assertion succeeded, FALSE otherwise.
  191. */
  192. public function assertRowStatus($result, $status, $message = '', $group = 'Node import') {
  193. return $this->assert(is_array($result) && isset($result['status']) && $result['status'] == $status, $message ? $message : t('Row status set to %d', array('%d' => $status)), $group);
  194. }
  195. /**
  196. * Check to see if the row status is NODE_IMPORT_STATUS_DONE.
  197. *
  198. * @param $result
  199. * Record from {node_import_status}.
  200. *
  201. * @param $message
  202. * Message to display along with the assertion.
  203. *
  204. * @param $group
  205. * The type of assertion - examples are "Browser", "PHP".
  206. *
  207. * @return
  208. * TRUE if the assertion succeeded, FALSE otherwise.
  209. */
  210. public function assertRowStatusDONE($result, $message = '', $group = 'Node import') {
  211. return $this->assertRowStatus($result, NODE_IMPORT_STATUS_DONE, $message ? $message : t('Row status set to DONE.'), $group);
  212. }
  213. /**
  214. * Check to see if the row status is NODE_IMPORT_STATUS_ERROR.
  215. *
  216. * @param $result
  217. * Record from {node_import_status}.
  218. *
  219. * @param $message
  220. * Message to display along with the assertion.
  221. *
  222. * @param $group
  223. * The type of assertion - examples are "Browser", "PHP".
  224. *
  225. * @return
  226. * TRUE if the assertion succeeded, FALSE otherwise.
  227. */
  228. public function assertRowStatusERROR($result, $message = '', $group = 'Node import') {
  229. return $this->assertRowStatus($result, NODE_IMPORT_STATUS_ERROR, $message ? $message : t('Row status set to ERROR.'), $group);
  230. }
  231. /**
  232. * Check to see if the row status is NODE_IMPORT_STATUS_PENDING.
  233. *
  234. * @param $result
  235. * Record from {node_import_status}.
  236. *
  237. * @param $message
  238. * Message to display along with the assertion.
  239. *
  240. * @param $group
  241. * The type of assertion - examples are "Browser", "PHP".
  242. *
  243. * @return
  244. * TRUE if the assertion succeeded, FALSE otherwise.
  245. */
  246. public function assertRowStatusPENDING($result, $message = '', $group = 'Node import') {
  247. return $this->assertRowStatus($result, NODE_IMPORT_STATUS_PENDING, $message ? $message : t('Row status set to PENDING.'), $group);
  248. }
  249. public function assertRowErrorContains($result, $error, $message = '', $group = 'Node import') {
  250. $errors = is_array($result['errors']) ? $result['errors'] : unserialize($result['errors']);
  251. return $this->assert(in_array($error, $errors, TRUE), $message ? $message : t('Row errors contains error message.'), $group);
  252. }
  253. }