PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/simpletest/drupal_web_test_case.php

https://github.com/jacobSingh/drupal
PHP | 2767 lines | 1188 code | 228 blank | 1351 comment | 181 complexity | e68609ca32c0feefd59967f83425a5e1 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // $Id: drupal_web_test_case.php,v 1.188 2010-01-04 23:08:34 webchick Exp $
  3. /**
  4. * Base class for Drupal tests.
  5. *
  6. * Do not extend this class, use one of the subclasses in this file.
  7. */
  8. abstract class DrupalTestCase {
  9. /**
  10. * The test run ID.
  11. *
  12. * @var string
  13. */
  14. protected $testId;
  15. /**
  16. * The original database prefix, before it was changed for testing purposes.
  17. *
  18. * @var string
  19. */
  20. protected $originalPrefix = NULL;
  21. /**
  22. * The original file directory, before it was changed for testing purposes.
  23. *
  24. * @var string
  25. */
  26. protected $originalFileDirectory = NULL;
  27. /**
  28. * Time limit for the test.
  29. */
  30. protected $timeLimit = 500;
  31. /**
  32. * Current results of this test case.
  33. *
  34. * @var Array
  35. */
  36. public $results = array(
  37. '#pass' => 0,
  38. '#fail' => 0,
  39. '#exception' => 0,
  40. '#debug' => 0,
  41. );
  42. /**
  43. * Assertions thrown in that test case.
  44. *
  45. * @var Array
  46. */
  47. protected $assertions = array();
  48. /**
  49. * This class is skipped when looking for the source of an assertion.
  50. *
  51. * When displaying which function an assert comes from, it's not too useful
  52. * to see "drupalWebTestCase->drupalLogin()', we would like to see the test
  53. * that called it. So we need to skip the classes defining these helper
  54. * methods.
  55. */
  56. protected $skipClasses = array(__CLASS__ => TRUE);
  57. /**
  58. * Constructor for DrupalWebTestCase.
  59. *
  60. * @param $test_id
  61. * Tests with the same id are reported together.
  62. */
  63. public function __construct($test_id = NULL) {
  64. $this->testId = $test_id;
  65. }
  66. /**
  67. * Internal helper: stores the assert.
  68. *
  69. * @param $status
  70. * Can be 'pass', 'fail', 'exception'.
  71. * TRUE is a synonym for 'pass', FALSE for 'fail'.
  72. * @param $message
  73. * The message string.
  74. * @param $group
  75. * Which group this assert belongs to.
  76. * @param $caller
  77. * By default, the assert comes from a function whose name starts with
  78. * 'test'. Instead, you can specify where this assert originates from
  79. * by passing in an associative array as $caller. Key 'file' is
  80. * the name of the source file, 'line' is the line number and 'function'
  81. * is the caller function itself.
  82. */
  83. protected function assert($status, $message = '', $group = 'Other', array $caller = NULL) {
  84. global $db_prefix;
  85. // Convert boolean status to string status.
  86. if (is_bool($status)) {
  87. $status = $status ? 'pass' : 'fail';
  88. }
  89. // Increment summary result counter.
  90. $this->results['#' . $status]++;
  91. // Get the function information about the call to the assertion method.
  92. if (!$caller) {
  93. $caller = $this->getAssertionCall();
  94. }
  95. // Switch to non-testing database to store results in.
  96. $current_db_prefix = $db_prefix;
  97. $db_prefix = $this->originalPrefix;
  98. // Creation assertion array that can be displayed while tests are running.
  99. $this->assertions[] = $assertion = array(
  100. 'test_id' => $this->testId,
  101. 'test_class' => get_class($this),
  102. 'status' => $status,
  103. 'message' => $message,
  104. 'message_group' => $group,
  105. 'function' => $caller['function'],
  106. 'line' => $caller['line'],
  107. 'file' => $caller['file'],
  108. );
  109. // Store assertion for display after the test has completed.
  110. db_insert('simpletest')
  111. ->fields($assertion)
  112. ->execute();
  113. // Return to testing prefix.
  114. $db_prefix = $current_db_prefix;
  115. // We do not use a ternary operator here to allow a breakpoint on
  116. // test failure.
  117. if ($status == 'pass') {
  118. return TRUE;
  119. }
  120. else {
  121. return FALSE;
  122. }
  123. }
  124. /**
  125. * Store an assertion from outside the testing context.
  126. *
  127. * This is useful for inserting assertions that can only be recorded after
  128. * the test case has been destroyed, such as PHP fatal errors. The caller
  129. * information is not automatically gathered since the caller is most likely
  130. * inserting the assertion on behalf of other code. In all other respects
  131. * the method behaves just like DrupalTestCase::assert() in terms of storing
  132. * the assertion.
  133. *
  134. * @return
  135. * Message ID of the stored assertion.
  136. *
  137. * @see DrupalTestCase::assert()
  138. * @see DrupalTestCase::deleteAssert()
  139. */
  140. public static function insertAssert($test_id, $test_class, $status, $message = '', $group = 'Other', array $caller = array()) {
  141. // Convert boolean status to string status.
  142. if (is_bool($status)) {
  143. $status = $status ? 'pass' : 'fail';
  144. }
  145. $caller += array(
  146. 'function' => t('Unknown'),
  147. 'line' => 0,
  148. 'file' => t('Unknown'),
  149. );
  150. $assertion = array(
  151. 'test_id' => $test_id,
  152. 'test_class' => $test_class,
  153. 'status' => $status,
  154. 'message' => $message,
  155. 'message_group' => $group,
  156. 'function' => $caller['function'],
  157. 'line' => $caller['line'],
  158. 'file' => $caller['file'],
  159. );
  160. return db_insert('simpletest')
  161. ->fields($assertion)
  162. ->execute();
  163. }
  164. /**
  165. * Delete an assertion record by message ID.
  166. *
  167. * @param $message_id
  168. * Message ID of the assertion to delete.
  169. * @return
  170. * TRUE if the assertion was deleted, FALSE otherwise.
  171. *
  172. * @see DrupalTestCase::insertAssert()
  173. */
  174. public static function deleteAssert($message_id) {
  175. return (bool) db_delete('simpletest')
  176. ->condition('message_id', $message_id)
  177. ->execute();
  178. }
  179. /**
  180. * Cycles through backtrace until the first non-assertion method is found.
  181. *
  182. * @return
  183. * Array representing the true caller.
  184. */
  185. protected function getAssertionCall() {
  186. $backtrace = debug_backtrace();
  187. // The first element is the call. The second element is the caller.
  188. // We skip calls that occurred in one of the methods of our base classes
  189. // or in an assertion function.
  190. while (($caller = $backtrace[1]) &&
  191. ((isset($caller['class']) && isset($this->skipClasses[$caller['class']])) ||
  192. substr($caller['function'], 0, 6) == 'assert')) {
  193. // We remove that call.
  194. array_shift($backtrace);
  195. }
  196. return _drupal_get_last_caller($backtrace);
  197. }
  198. /**
  199. * Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
  200. *
  201. * @param $value
  202. * The value on which the assertion is to be done.
  203. * @param $message
  204. * The message to display along with the assertion.
  205. * @param $group
  206. * The type of assertion - examples are "Browser", "PHP".
  207. * @return
  208. * TRUE if the assertion succeeded, FALSE otherwise.
  209. */
  210. protected function assertTrue($value, $message = '', $group = 'Other') {
  211. return $this->assert((bool) $value, $message ? $message : t('Value @value is TRUE.', array('@value' => var_export($value, TRUE))), $group);
  212. }
  213. /**
  214. * Check to see if a value is false (an empty string, 0, NULL, or FALSE).
  215. *
  216. * @param $value
  217. * The value on which the assertion is to be done.
  218. * @param $message
  219. * The message to display along with the assertion.
  220. * @param $group
  221. * The type of assertion - examples are "Browser", "PHP".
  222. * @return
  223. * TRUE if the assertion succeeded, FALSE otherwise.
  224. */
  225. protected function assertFalse($value, $message = '', $group = 'Other') {
  226. return $this->assert(!$value, $message ? $message : t('Value @value is FALSE.', array('@value' => var_export($value, TRUE))), $group);
  227. }
  228. /**
  229. * Check to see if a value is NULL.
  230. *
  231. * @param $value
  232. * The value on which the assertion is to be done.
  233. * @param $message
  234. * The message to display along with the assertion.
  235. * @param $group
  236. * The type of assertion - examples are "Browser", "PHP".
  237. * @return
  238. * TRUE if the assertion succeeded, FALSE otherwise.
  239. */
  240. protected function assertNull($value, $message = '', $group = 'Other') {
  241. return $this->assert(!isset($value), $message ? $message : t('Value @value is NULL.', array('@value' => var_export($value, TRUE))), $group);
  242. }
  243. /**
  244. * Check to see if a value is not NULL.
  245. *
  246. * @param $value
  247. * The value on which the assertion is to be done.
  248. * @param $message
  249. * The message to display along with the assertion.
  250. * @param $group
  251. * The type of assertion - examples are "Browser", "PHP".
  252. * @return
  253. * TRUE if the assertion succeeded, FALSE otherwise.
  254. */
  255. protected function assertNotNull($value, $message = '', $group = 'Other') {
  256. return $this->assert(isset($value), $message ? $message : t('Value @value is not NULL.', array('@value' => var_export($value, TRUE))), $group);
  257. }
  258. /**
  259. * Check to see if two values are equal.
  260. *
  261. * @param $first
  262. * The first value to check.
  263. * @param $second
  264. * The second value to check.
  265. * @param $message
  266. * The message to display along with the assertion.
  267. * @param $group
  268. * The type of assertion - examples are "Browser", "PHP".
  269. * @return
  270. * TRUE if the assertion succeeded, FALSE otherwise.
  271. */
  272. protected function assertEqual($first, $second, $message = '', $group = 'Other') {
  273. return $this->assert($first == $second, $message ? $message : t('Value @first is equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
  274. }
  275. /**
  276. * Check to see if two values are not equal.
  277. *
  278. * @param $first
  279. * The first value to check.
  280. * @param $second
  281. * The second value to check.
  282. * @param $message
  283. * The message to display along with the assertion.
  284. * @param $group
  285. * The type of assertion - examples are "Browser", "PHP".
  286. * @return
  287. * TRUE if the assertion succeeded, FALSE otherwise.
  288. */
  289. protected function assertNotEqual($first, $second, $message = '', $group = 'Other') {
  290. return $this->assert($first != $second, $message ? $message : t('Value @first is not equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
  291. }
  292. /**
  293. * Check to see if two values are identical.
  294. *
  295. * @param $first
  296. * The first value to check.
  297. * @param $second
  298. * The second value to check.
  299. * @param $message
  300. * The message to display along with the assertion.
  301. * @param $group
  302. * The type of assertion - examples are "Browser", "PHP".
  303. * @return
  304. * TRUE if the assertion succeeded, FALSE otherwise.
  305. */
  306. protected function assertIdentical($first, $second, $message = '', $group = 'Other') {
  307. return $this->assert($first === $second, $message ? $message : t('Value @first is identical to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
  308. }
  309. /**
  310. * Check to see if two values are not identical.
  311. *
  312. * @param $first
  313. * The first value to check.
  314. * @param $second
  315. * The second value to check.
  316. * @param $message
  317. * The message to display along with the assertion.
  318. * @param $group
  319. * The type of assertion - examples are "Browser", "PHP".
  320. * @return
  321. * TRUE if the assertion succeeded, FALSE otherwise.
  322. */
  323. protected function assertNotIdentical($first, $second, $message = '', $group = 'Other') {
  324. return $this->assert($first !== $second, $message ? $message : t('Value @first is not identical to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
  325. }
  326. /**
  327. * Fire an assertion that is always positive.
  328. *
  329. * @param $message
  330. * The message to display along with the assertion.
  331. * @param $group
  332. * The type of assertion - examples are "Browser", "PHP".
  333. * @return
  334. * TRUE.
  335. */
  336. protected function pass($message = NULL, $group = 'Other') {
  337. return $this->assert(TRUE, $message, $group);
  338. }
  339. /**
  340. * Fire an assertion that is always negative.
  341. *
  342. * @param $message
  343. * The message to display along with the assertion.
  344. * @param $group
  345. * The type of assertion - examples are "Browser", "PHP".
  346. * @return
  347. * FALSE.
  348. */
  349. protected function fail($message = NULL, $group = 'Other') {
  350. return $this->assert(FALSE, $message, $group);
  351. }
  352. /**
  353. * Fire an error assertion.
  354. *
  355. * @param $message
  356. * The message to display along with the assertion.
  357. * @param $group
  358. * The type of assertion - examples are "Browser", "PHP".
  359. * @param $caller
  360. * The caller of the error.
  361. * @return
  362. * FALSE.
  363. */
  364. protected function error($message = '', $group = 'Other', array $caller = NULL) {
  365. if ($group == 'User notice') {
  366. // Since 'User notice' is set by trigger_error() which is used for debug
  367. // set the message to a status of 'debug'.
  368. return $this->assert('debug', $message, 'Debug', $caller);
  369. }
  370. return $this->assert('exception', $message, $group, $caller);
  371. }
  372. /**
  373. * Run all tests in this class.
  374. */
  375. public function run() {
  376. // Initialize verbose debugging.
  377. simpletest_verbose(NULL, file_directory_path(), get_class($this));
  378. // HTTP auth settings (<username>:<password>) for the simpletest browser
  379. // when sending requests to the test site.
  380. $this->httpauth_method = variable_get('simpletest_httpauth_method', CURLAUTH_BASIC);
  381. $username = variable_get('simpletest_httpauth_username', NULL);
  382. $password = variable_get('simpletest_httpauth_password', NULL);
  383. if ($username && $password) {
  384. $this->httpauth_credentials = $username . ':' . $password;
  385. }
  386. set_error_handler(array($this, 'errorHandler'));
  387. $class = get_class($this);
  388. // Iterate through all the methods in this class.
  389. foreach (get_class_methods($class) as $method) {
  390. // If the current method starts with "test", run it - it's a test.
  391. if (strtolower(substr($method, 0, 4)) == 'test') {
  392. // Insert a fail record. This will be deleted on completion to ensure
  393. // that testing completed.
  394. $method_info = new ReflectionMethod($class, $method);
  395. $caller = array(
  396. 'file' => $method_info->getFileName(),
  397. 'line' => $method_info->getStartLine(),
  398. 'function' => $class . '->' . $method . '()',
  399. );
  400. $completion_check_id = DrupalTestCase::insertAssert($this->testId, $class, FALSE, t('The test did not complete due to a fatal error.'), 'Completion check', $caller);
  401. $this->setUp();
  402. try {
  403. $this->$method();
  404. // Finish up.
  405. }
  406. catch (Exception $e) {
  407. $this->exceptionHandler($e);
  408. }
  409. $this->tearDown();
  410. // Remove the completion check record.
  411. DrupalTestCase::deleteAssert($completion_check_id);
  412. }
  413. }
  414. // Clear out the error messages and restore error handler.
  415. drupal_get_messages();
  416. restore_error_handler();
  417. }
  418. /**
  419. * Handle errors during test runs.
  420. *
  421. * Because this is registered in set_error_handler(), it has to be public.
  422. * @see set_error_handler
  423. */
  424. public function errorHandler($severity, $message, $file = NULL, $line = NULL) {
  425. if ($severity & error_reporting()) {
  426. $error_map = array(
  427. E_STRICT => 'Run-time notice',
  428. E_WARNING => 'Warning',
  429. E_NOTICE => 'Notice',
  430. E_CORE_ERROR => 'Core error',
  431. E_CORE_WARNING => 'Core warning',
  432. E_USER_ERROR => 'User error',
  433. E_USER_WARNING => 'User warning',
  434. E_USER_NOTICE => 'User notice',
  435. E_RECOVERABLE_ERROR => 'Recoverable error',
  436. );
  437. $backtrace = debug_backtrace();
  438. $this->error($message, $error_map[$severity], _drupal_get_last_caller($backtrace));
  439. }
  440. return TRUE;
  441. }
  442. /**
  443. * Handle exceptions.
  444. *
  445. * @see set_exception_handler
  446. */
  447. protected function exceptionHandler($exception) {
  448. $backtrace = $exception->getTrace();
  449. // Push on top of the backtrace the call that generated the exception.
  450. array_unshift($backtrace, array(
  451. 'line' => $exception->getLine(),
  452. 'file' => $exception->getFile(),
  453. ));
  454. $this->error($exception->getMessage(), 'Uncaught exception', _drupal_get_last_caller($backtrace));
  455. }
  456. /**
  457. * Generates a random string of ASCII characters of codes 32 to 126.
  458. *
  459. * The generated string includes alpha-numeric characters and common misc
  460. * characters. Use this method when testing general input where the content
  461. * is not restricted.
  462. *
  463. * @param $length
  464. * Length of random string to generate which will be appended to $db_prefix.
  465. * @return
  466. * Randomly generated string.
  467. */
  468. public static function randomString($length = 8) {
  469. global $db_prefix;
  470. $str = '';
  471. for ($i = 0; $i < $length; $i++) {
  472. $str .= chr(mt_rand(32, 126));
  473. }
  474. return str_replace('simpletest', 's', $db_prefix) . $str;
  475. }
  476. /**
  477. * Generates a random string containing letters and numbers.
  478. *
  479. * The letters may be upper or lower case. This method is better for
  480. * restricted inputs that do not accept certain characters. For example,
  481. * when testing input fields that require machine readable values (ie without
  482. * spaces and non-standard characters) this method is best.
  483. *
  484. * @param $length
  485. * Length of random string to generate which will be appended to $db_prefix.
  486. * @return
  487. * Randomly generated string.
  488. */
  489. public static function randomName($length = 8) {
  490. global $db_prefix;
  491. $values = array_merge(range(65, 90), range(97, 122), range(48, 57));
  492. $max = count($values) - 1;
  493. $str = '';
  494. for ($i = 0; $i < $length; $i++) {
  495. $str .= chr($values[mt_rand(0, $max)]);
  496. }
  497. return str_replace('simpletest', 's', $db_prefix) . $str;
  498. }
  499. }
  500. /**
  501. * Test case for Drupal unit tests.
  502. *
  503. * These tests can not access the database nor files. Calling any Drupal
  504. * function that needs the database will throw exceptions. These include
  505. * watchdog(), function_exists(), module_implements(),
  506. * module_invoke_all() etc.
  507. */
  508. class DrupalUnitTestCase extends DrupalTestCase {
  509. /**
  510. * Constructor for DrupalUnitTestCase.
  511. */
  512. function __construct($test_id = NULL) {
  513. parent::__construct($test_id);
  514. $this->skipClasses[__CLASS__] = TRUE;
  515. }
  516. protected function setUp() {
  517. global $db_prefix, $conf;
  518. // Store necessary current values before switching to prefixed database.
  519. $this->originalPrefix = $db_prefix;
  520. $this->originalFileDirectory = file_directory_path();
  521. // Reset all statics so that test is performed with a clean environment.
  522. drupal_static_reset();
  523. // Generate temporary prefixed database to ensure that tests have a clean starting point.
  524. $db_prefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
  525. $conf['file_public_path'] = $this->originalFileDirectory . '/' . $db_prefix;
  526. // If locale is enabled then t() will try to access the database and
  527. // subsequently will fail as the database is not accessible.
  528. $module_list = module_list();
  529. if (isset($module_list['locale'])) {
  530. $this->originalModuleList = $module_list;
  531. unset($module_list['locale']);
  532. module_list(TRUE, FALSE, FALSE, $module_list);
  533. }
  534. }
  535. protected function tearDown() {
  536. global $db_prefix, $conf;
  537. if (preg_match('/simpletest\d+/', $db_prefix)) {
  538. $conf['file_public_path'] = $this->originalFileDirectory;
  539. // Return the database prefix to the original.
  540. $db_prefix = $this->originalPrefix;
  541. // Restore modules if necessary.
  542. if (isset($this->originalModuleList)) {
  543. module_list(TRUE, FALSE, FALSE, $this->originalModuleList);
  544. }
  545. }
  546. }
  547. }
  548. /**
  549. * Test case for typical Drupal tests.
  550. */
  551. class DrupalWebTestCase extends DrupalTestCase {
  552. /**
  553. * The URL currently loaded in the internal browser.
  554. *
  555. * @var string
  556. */
  557. protected $url;
  558. /**
  559. * The handle of the current cURL connection.
  560. *
  561. * @var resource
  562. */
  563. protected $curlHandle;
  564. /**
  565. * The headers of the page currently loaded in the internal browser.
  566. *
  567. * @var Array
  568. */
  569. protected $headers;
  570. /**
  571. * The content of the page currently loaded in the internal browser.
  572. *
  573. * @var string
  574. */
  575. protected $content;
  576. /**
  577. * The content of the page currently loaded in the internal browser (plain text version).
  578. *
  579. * @var string
  580. */
  581. protected $plainTextContent;
  582. /**
  583. * The parsed version of the page.
  584. *
  585. * @var SimpleXMLElement
  586. */
  587. protected $elements = NULL;
  588. /**
  589. * The current user logged in using the internal browser.
  590. *
  591. * @var bool
  592. */
  593. protected $loggedInUser = FALSE;
  594. /**
  595. * The current cookie file used by cURL.
  596. *
  597. * We do not reuse the cookies in further runs, so we do not need a file
  598. * but we still need cookie handling, so we set the jar to NULL.
  599. */
  600. protected $cookieFile = NULL;
  601. /**
  602. * Additional cURL options.
  603. *
  604. * DrupalWebTestCase itself never sets this but always obeys what is set.
  605. */
  606. protected $additionalCurlOptions = array();
  607. /**
  608. * The original user, before it was changed to a clean uid = 1 for testing purposes.
  609. *
  610. * @var object
  611. */
  612. protected $originalUser = NULL;
  613. /**
  614. * HTTP authentication method
  615. */
  616. protected $httpauth_method = CURLAUTH_BASIC;
  617. /**
  618. * HTTP authentication credentials (<username>:<password>).
  619. */
  620. protected $httpauth_credentials = NULL;
  621. /**
  622. * The current session name, if available.
  623. */
  624. protected $session_name = NULL;
  625. /**
  626. * The current session ID, if available.
  627. */
  628. protected $session_id = NULL;
  629. /**
  630. * Whether the files were copied to the test files directory.
  631. */
  632. protected $generatedTestFiles = FALSE;
  633. /**
  634. * Constructor for DrupalWebTestCase.
  635. */
  636. function __construct($test_id = NULL) {
  637. parent::__construct($test_id);
  638. $this->skipClasses[__CLASS__] = TRUE;
  639. }
  640. /**
  641. * Get a node from the database based on its title.
  642. *
  643. * @param title
  644. * A node title, usually generated by $this->randomName().
  645. *
  646. * @return
  647. * A node object matching $title.
  648. */
  649. function drupalGetNodeByTitle($title) {
  650. $nodes = node_load_multiple(array(), array('title' => $title));
  651. // Load the first node returned from the database.
  652. $returned_node = reset($nodes);
  653. return $returned_node;
  654. }
  655. /**
  656. * Creates a node based on default settings.
  657. *
  658. * @param $settings
  659. * An associative array of settings to change from the defaults, keys are
  660. * node properties, for example 'title' => 'Hello, world!'.
  661. * @return
  662. * Created node object.
  663. */
  664. protected function drupalCreateNode($settings = array()) {
  665. // Populate defaults array.
  666. $settings += array(
  667. 'body' => array(LANGUAGE_NONE => array(array())),
  668. 'title' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(8)))),
  669. 'comment' => 2,
  670. 'changed' => REQUEST_TIME,
  671. 'moderate' => 0,
  672. 'promote' => 0,
  673. 'revision' => 1,
  674. 'log' => '',
  675. 'status' => 1,
  676. 'sticky' => 0,
  677. 'type' => 'page',
  678. 'revisions' => NULL,
  679. 'taxonomy' => NULL,
  680. 'language' => LANGUAGE_NONE,
  681. );
  682. // Use the original node's created time for existing nodes.
  683. if (isset($settings['created']) && !isset($settings['date'])) {
  684. $settings['date'] = format_date($settings['created'], 'custom', 'Y-m-d H:i:s O');
  685. }
  686. // If the node's user uid is not specified manually, use the currently
  687. // logged in user if available, or else the user running the test.
  688. if (!isset($settings['uid'])) {
  689. if ($this->loggedInUser) {
  690. $settings['uid'] = $this->loggedInUser->uid;
  691. }
  692. else {
  693. global $user;
  694. $settings['uid'] = $user->uid;
  695. }
  696. }
  697. // Merge body field value and format separately.
  698. $body = array(
  699. 'value' => $this->randomName(32),
  700. 'format' => filter_default_format(),
  701. );
  702. $settings['body'][$settings['language']][0] += $body;
  703. $node = (object) $settings;
  704. node_save($node);
  705. // Small hack to link revisions to our test user.
  706. db_update('node_revision')
  707. ->fields(array('uid' => $node->uid))
  708. ->condition('vid', $node->vid)
  709. ->execute();
  710. return $node;
  711. }
  712. /**
  713. * Creates a custom content type based on default settings.
  714. *
  715. * @param $settings
  716. * An array of settings to change from the defaults.
  717. * Example: 'type' => 'foo'.
  718. * @return
  719. * Created content type.
  720. */
  721. protected function drupalCreateContentType($settings = array()) {
  722. // Find a non-existent random type name.
  723. do {
  724. $name = strtolower($this->randomName(8));
  725. } while (node_type_get_type($name));
  726. // Populate defaults array.
  727. $defaults = array(
  728. 'type' => $name,
  729. 'name' => $name,
  730. 'base' => 'node_content',
  731. 'description' => '',
  732. 'help' => '',
  733. 'title_label' => 'Title',
  734. 'body_label' => 'Body',
  735. 'has_title' => 1,
  736. 'has_body' => 1,
  737. );
  738. // Imposed values for a custom type.
  739. $forced = array(
  740. 'orig_type' => '',
  741. 'old_type' => '',
  742. 'module' => 'node',
  743. 'custom' => 1,
  744. 'modified' => 1,
  745. 'locked' => 0,
  746. );
  747. $type = $forced + $settings + $defaults;
  748. $type = (object)$type;
  749. $saved_type = node_type_save($type);
  750. node_types_rebuild();
  751. menu_rebuild();
  752. $this->assertEqual($saved_type, SAVED_NEW, t('Created content type %type.', array('%type' => $type->type)));
  753. // Reset permissions so that permissions for this content type are available.
  754. $this->checkPermissions(array(), TRUE);
  755. return $type;
  756. }
  757. /**
  758. * Get a list files that can be used in tests.
  759. *
  760. * @param $type
  761. * File type, possible values: 'binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'.
  762. * @param $size
  763. * File size in bytes to match. Please check the tests/files folder.
  764. * @return
  765. * List of files that match filter.
  766. */
  767. protected function drupalGetTestFiles($type, $size = NULL) {
  768. if (empty($this->generatedTestFiles)) {
  769. // Generate binary test files.
  770. $lines = array(64, 1024);
  771. $count = 0;
  772. foreach ($lines as $line) {
  773. simpletest_generate_file('binary-' . $count++, 64, $line, 'binary');
  774. }
  775. // Generate text test files.
  776. $lines = array(16, 256, 1024, 2048, 20480);
  777. $count = 0;
  778. foreach ($lines as $line) {
  779. simpletest_generate_file('text-' . $count++, 64, $line);
  780. }
  781. // Copy other test files from simpletest.
  782. $original = drupal_get_path('module', 'simpletest') . '/files';
  783. $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
  784. $destination_path = file_directory_path('public');
  785. foreach ($files as $file) {
  786. file_unmanaged_copy($file->uri, $destination_path);
  787. }
  788. $this->generatedTestFiles = TRUE;
  789. }
  790. $files = array();
  791. // Make sure type is valid.
  792. if (in_array($type, array('binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'))) {
  793. $files = file_scan_directory(file_directory_path('public'), '/' . $type . '\-.*/');
  794. // If size is set then remove any files that are not of that size.
  795. if ($size !== NULL) {
  796. foreach ($files as $file) {
  797. $stats = stat($file->uri);
  798. if ($stats['size'] != $size) {
  799. unset($files[$file->uri]);
  800. }
  801. }
  802. }
  803. }
  804. usort($files, array($this, 'drupalCompareFiles'));
  805. return $files;
  806. }
  807. /**
  808. * Compare two files based on size and file name.
  809. */
  810. protected function drupalCompareFiles($file1, $file2) {
  811. $compare_size = filesize($file1->uri) - filesize($file2->uri);
  812. if ($compare_size) {
  813. // Sort by file size.
  814. return $compare_size;
  815. }
  816. else {
  817. // The files were the same size, so sort alphabetically.
  818. return strnatcmp($file1->name, $file2->name);
  819. }
  820. }
  821. /**
  822. * Create a user with a given set of permissions. The permissions correspond to the
  823. * names given on the privileges page.
  824. *
  825. * @param $permissions
  826. * Array of permission names to assign to user.
  827. * @return
  828. * A fully loaded user object with pass_raw property, or FALSE if account
  829. * creation fails.
  830. */
  831. protected function drupalCreateUser($permissions = array('access comments', 'access content', 'post comments', 'post comments without approval')) {
  832. // Create a role with the given permission set.
  833. if (!($rid = $this->drupalCreateRole($permissions))) {
  834. return FALSE;
  835. }
  836. // Create a user assigned to that role.
  837. $edit = array();
  838. $edit['name'] = $this->randomName();
  839. $edit['mail'] = $edit['name'] . '@example.com';
  840. $edit['roles'] = array($rid => $rid);
  841. $edit['pass'] = user_password();
  842. $edit['status'] = 1;
  843. $account = user_save(drupal_anonymous_user(), $edit);
  844. $this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
  845. if (empty($account->uid)) {
  846. return FALSE;
  847. }
  848. // Add the raw password so that we can log in as this user.
  849. $account->pass_raw = $edit['pass'];
  850. return $account;
  851. }
  852. /**
  853. * Internal helper function; Create a role with specified permissions.
  854. *
  855. * @param $permissions
  856. * Array of permission names to assign to role.
  857. * @param $name
  858. * (optional) String for the name of the role. Defaults to a random string.
  859. * @return
  860. * Role ID of newly created role, or FALSE if role creation failed.
  861. */
  862. protected function drupalCreateRole(array $permissions, $name = NULL) {
  863. // Generate random name if it was not passed.
  864. if (!$name) {
  865. $name = $this->randomName();
  866. }
  867. // Check the all the permissions strings are valid.
  868. if (!$this->checkPermissions($permissions)) {
  869. return FALSE;
  870. }
  871. // Create new role.
  872. $role = new stdClass();
  873. $role->name = $name;
  874. user_role_save($role);
  875. user_role_grant_permissions($role->rid, $permissions);
  876. $this->assertTrue(isset($role->rid), t('Created role of name: @name, id: @rid', array('@name' => $name, '@rid' => (isset($role->rid) ? $role->rid : t('-n/a-')))), t('Role'));
  877. if ($role && !empty($role->rid)) {
  878. $count = db_query('SELECT COUNT(*) FROM {role_permission} WHERE rid = :rid', array(':rid' => $role->rid))->fetchField();
  879. $this->assertTrue($count == count($permissions), t('Created permissions: @perms', array('@perms' => implode(', ', $permissions))), t('Role'));
  880. return $role->rid;
  881. }
  882. else {
  883. return FALSE;
  884. }
  885. }
  886. /**
  887. * Check to make sure that the array of permissions are valid.
  888. *
  889. * @param $permissions
  890. * Permissions to check.
  891. * @param $reset
  892. * Reset cached available permissions.
  893. * @return
  894. * TRUE or FALSE depending on whether the permissions are valid.
  895. */
  896. protected function checkPermissions(array $permissions, $reset = FALSE) {
  897. $available = &drupal_static(__FUNCTION__);
  898. if (!isset($available) || $reset) {
  899. $available = array_keys(module_invoke_all('permission'));
  900. }
  901. $valid = TRUE;
  902. foreach ($permissions as $permission) {
  903. if (!in_array($permission, $available)) {
  904. $this->fail(t('Invalid permission %permission.', array('%permission' => $permission)), t('Role'));
  905. $valid = FALSE;
  906. }
  907. }
  908. return $valid;
  909. }
  910. /**
  911. * Log in a user with the internal browser.
  912. *
  913. * If a user is already logged in, then the current user is logged out before
  914. * logging in the specified user.
  915. *
  916. * Please note that neither the global $user nor the passed in user object is
  917. * populated with data of the logged in user. If you need full access to the
  918. * user object after logging in, it must be updated manually. If you also need
  919. * access to the plain-text password of the user (set by drupalCreateUser()),
  920. * e.g. to login the same user again, then it must be re-assigned manually.
  921. * For example:
  922. * @code
  923. * // Create a user.
  924. * $account = $this->drupalCreateUser(array());
  925. * $this->drupalLogin($account);
  926. * // Load real user object.
  927. * $pass_raw = $account->pass_raw;
  928. * $account = user_load($account->uid);
  929. * $account->pass_raw = $pass_raw;
  930. * @endcode
  931. *
  932. * @param $user
  933. * User object representing the user to login.
  934. *
  935. * @see drupalCreateUser()
  936. */
  937. protected function drupalLogin(stdClass $user) {
  938. if ($this->loggedInUser) {
  939. $this->drupalLogout();
  940. }
  941. $edit = array(
  942. 'name' => $user->name,
  943. 'pass' => $user->pass_raw
  944. );
  945. $this->drupalPost('user', $edit, t('Log in'));
  946. // If a "log out" link appears on the page, it is almost certainly because
  947. // the login was successful.
  948. $pass = $this->assertLink(t('Log out'), 0, t('User %name successfully logged in.', array('%name' => $user->name)), t('User login'));
  949. if ($pass) {
  950. $this->loggedInUser = $user;
  951. }
  952. }
  953. /**
  954. * Generate a token for the currently logged in user.
  955. */
  956. protected function drupalGetToken($value = '') {
  957. $private_key = drupal_get_private_key();
  958. return md5($this->session_id . $value . $private_key);
  959. }
  960. /*
  961. * Logs a user out of the internal browser, then check the login page to confirm logout.
  962. */
  963. protected function drupalLogout() {
  964. // Make a request to the logout page, and redirect to the user page, the
  965. // idea being if you were properly logged out you should be seeing a login
  966. // screen.
  967. $this->drupalGet('user/logout', array('query' => array('destination' => 'user')));
  968. $pass = $this->assertField('name', t('Username field found.'), t('Logout'));
  969. $pass = $pass && $this->assertField('pass', t('Password field found.'), t('Logout'));
  970. if ($pass) {
  971. $this->loggedInUser = FALSE;
  972. }
  973. }
  974. /**
  975. * Generates a random database prefix, runs the install scripts on the
  976. * prefixed database and enable the specified modules. After installation
  977. * many caches are flushed and the internal browser is setup so that the
  978. * page requests will run on the new prefix. A temporary files directory
  979. * is created with the same name as the database prefix.
  980. *
  981. * @param ...
  982. * List of modules to enable for the duration of the test.
  983. */
  984. protected function setUp() {
  985. global $db_prefix, $user, $language;
  986. // Store necessary current values before switching to prefixed database.
  987. $this->originalLanguage = $language;
  988. $this->originalLanguageDefault = variable_get('language_default');
  989. $this->originalPrefix = $db_prefix;
  990. $this->originalFileDirectory = file_directory_path();
  991. $this->originalProfile = drupal_get_profile();
  992. $clean_url_original = variable_get('clean_url', 0);
  993. // Generate temporary prefixed database to ensure that tests have a clean starting point.
  994. $db_prefix_new = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
  995. db_update('simpletest_test_id')
  996. ->fields(array('last_prefix' => $db_prefix_new))
  997. ->condition('test_id', $this->testId)
  998. ->execute();
  999. $db_prefix = $db_prefix_new;
  1000. // Create test directory ahead of installation so fatal errors and debug
  1001. // information can be logged during installation process.
  1002. // Use temporary files directory with the same prefix as the database.
  1003. $public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($db_prefix, 10);
  1004. $private_files_directory = $public_files_directory . '/private';
  1005. $temp_files_directory = $private_files_directory . '/temp';
  1006. // Create the directories
  1007. file_prepare_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  1008. file_prepare_directory($private_files_directory, FILE_CREATE_DIRECTORY);
  1009. file_prepare_directory($temp_files_directory, FILE_CREATE_DIRECTORY);
  1010. $this->generatedTestFiles = FALSE;
  1011. // Log fatal errors.
  1012. ini_set('log_errors', 1);
  1013. ini_set('error_log', $public_files_directory . '/error.log');
  1014. // Reset all statics so that test is performed with a clean environment.
  1015. drupal_static_reset();
  1016. include_once DRUPAL_ROOT . '/includes/install.inc';
  1017. drupal_install_system();
  1018. $this->preloadRegistry();
  1019. // Include the default profile
  1020. variable_set('install_profile', 'standard');
  1021. $profile_details = install_profile_info('standard', 'en');
  1022. // Install the modules specified by the default profile.
  1023. drupal_install_modules($profile_details['dependencies'], TRUE);
  1024. drupal_static_reset('_node_types_build');
  1025. if ($modules = func_get_args()) {
  1026. // Install modules needed for this test.
  1027. drupal_install_modules($modules, TRUE);
  1028. }
  1029. // Because the schema is static cached, we need to flush
  1030. // it between each run. If we don't, then it will contain
  1031. // stale data for the previous run's database prefix and all
  1032. // calls to it will fail.
  1033. drupal_get_schema(NULL, TRUE);
  1034. // Run default profile tasks.
  1035. $install_state = array();
  1036. drupal_install_modules(array('standard'), TRUE);
  1037. // Rebuild caches.
  1038. node_types_rebuild();
  1039. actions_synchronize();
  1040. _drupal_flush_css_js();
  1041. $this->refreshVariables();
  1042. $this->checkPermissions(array(), TRUE);
  1043. // Log in with a clean $user.
  1044. $this->originalUser = $user;
  1045. drupal_save_session(FALSE);
  1046. $user = user_load(1);
  1047. // Restore necessary variables.
  1048. variable_set('install_task', 'done');
  1049. variable_set('clean_url', $clean_url_original);
  1050. variable_set('site_mail', 'simpletest@example.com');
  1051. // Set up English language.
  1052. unset($GLOBALS['conf']['language_default']);
  1053. $language = language_default();
  1054. // Set path variables
  1055. variable_set('file_public_path', $public_files_directory);
  1056. variable_set('file_private_path', $private_files_directory);
  1057. variable_set('file_temporary_path', $temp_files_directory);
  1058. // Use the test mail class instead of the default mail handler class.
  1059. variable_set('mail_system', array('default-system' => 'TestingMailSystem'));
  1060. drupal_set_time_limit($this->timeLimit);
  1061. }
  1062. /**
  1063. * This method is called by DrupalWebTestCase::setUp, and preloads the
  1064. * registry from the testing site to cut down on the time it takes to
  1065. * setup a clean environment for the current test run.
  1066. */
  1067. protected function preloadRegistry() {
  1068. db_query('INSERT INTO {registry} SELECT * FROM ' . $this->originalPrefix . 'registry');
  1069. db_query('INSERT INTO {registry_file} SELECT * FROM ' . $this->originalPrefix . 'registry_file');
  1070. }
  1071. /**
  1072. * Refresh the in-memory set of variables. Useful after a page request is made
  1073. * that changes a variable in a different thread.
  1074. *
  1075. * In other words calling a settings page with $this->drupalPost() with a changed
  1076. * value would update a variable to reflect that change, but in the thread that
  1077. * made the call (thread running the test) the changed variable would not be
  1078. * picked up.
  1079. *
  1080. * This method clears the variables cache and loads a fresh copy from the database
  1081. * to ensure that the most up-to-date set of variables is loaded.
  1082. */
  1083. protected function refreshVariables() {
  1084. global $conf;
  1085. cache_clear_all('variables', 'cache_bootstrap');
  1086. $conf = variable_initialize();
  1087. }
  1088. /**
  1089. * Delete created files and temporary files directory, delete the tables created by setUp(),
  1090. * and reset the database prefix.
  1091. */
  1092. protected function tearDown() {
  1093. global $db_prefix, $user, $language;
  1094. // In case a fatal error occured that was not in the test process read the
  1095. // log to pick up any fatal errors.
  1096. $db_prefix_temp = $db_prefix;
  1097. $db_prefix = $this->originalPrefix;
  1098. simpletest_log_read($this->testId, $db_prefix, get_class($this), TRUE);
  1099. $db_prefix = $db_prefix_temp;
  1100. $emailCount = count(variable_get('drupal_test_email_collector', array()));
  1101. if ($emailCount) {
  1102. $message = format_plural($emailCount, t('!count e-mail was sent during this test.'), t('!count e-mails were sent during this test.'), array('!count' => $emailCount));
  1103. $this->pass($message, t('E-mail'));
  1104. }
  1105. if (preg_match('/simpletest\d+/', $db_prefix)) {
  1106. // Delete temporary files directory.
  1107. file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($db_prefix, 10));
  1108. // Remove all prefixed tables (all the tables in the schema).
  1109. $schema = drupal_get_schema(NULL, TRUE);
  1110. $ret = array();
  1111. foreach ($schema as $name => $table) {
  1112. db_drop_table($name);
  1113. }
  1114. // Return the database prefix to the original.
  1115. $db_prefix = $this->originalPrefix;
  1116. // Return the user to the original one.
  1117. $user = $this->originalUser;
  1118. drupal_save_session(TRUE);
  1119. // Ensure that internal logged in variable and cURL options are reset.
  1120. $this->loggedInUser = FALSE;
  1121. $this->additionalCurlOptions = array();
  1122. // Reload module list and implementations to ensure that test module hooks
  1123. // aren't called after tests.
  1124. module_list(TRUE);
  1125. module_implements('', FALSE, TRUE);
  1126. // Reset the Field API.
  1127. field_cache_clear();
  1128. // Rebuild caches.
  1129. $this->refreshVariables();
  1130. // Reset language.
  1131. $language = $this->originalLanguage;
  1132. if ($this->originalLanguageDefault) {
  1133. $GLOBALS['conf']['language_default'] = $this->originalLanguageDefault;
  1134. }
  1135. // Close the CURL handler.
  1136. $this->curlClose();
  1137. }
  1138. }
  1139. /**
  1140. * Initializes the cURL connection.
  1141. *
  1142. * If the simpletest_httpauth_credentials variable is set, this function will
  1143. * add HTTP authentication headers. This is necessary for testing sites that
  1144. * are protected by login credentials from public access.
  1145. * See the description of $curl_options for other options.
  1146. */
  1147. protected function curlInitialize() {
  1148. global $base_url, $db_prefix;
  1149. if (!isset($this->curlHandle)) {
  1150. $this->curlHandle = curl_init();
  1151. $curl_options = $this->additionalCurlOptions + array(
  1152. CURLOPT_COOKIEJAR => $this->cookieFile,
  1153. CURLOPT_URL => $base_url,
  1154. CURLOPT_FOLLOWLOCATION => TRUE,
  1155. CURLOPT_MAXREDIRS => 5,
  1156. CURLOPT_RETURNTRANSFER => TRUE,
  1157. CURLOPT_SSL_VERIFYPEER => FALSE, // Required to make the tests run on https.
  1158. CURLOPT_SSL_VERIFYHOST => FALSE, // Required to make the tests run on https.
  1159. CURLOPT_HEADERFUNCTION => array(&$this, 'curlHeaderCallback'),
  1160. );
  1161. if (isset($this->httpauth_credentials)) {
  1162. $curl_options[CURLOPT_HTTPAUTH] = $this->httpauth_method;
  1163. $curl_options[CURLOPT_USERPWD] = $this->httpauth_credentials;
  1164. }
  1165. curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
  1166. // By default, the child session name should be the same as the parent.
  1167. $this->session_name = session_name();
  1168. }
  1169. // We set the user agent header on each request so as to use the current
  1170. // time and a new uniqid.
  1171. if (preg_match('/simpletest\d+/', $db_prefix, $matches)) {
  1172. curl_setopt($this->curlHandle, CURLOPT_USERAGENT, drupal_generate_test_ua($matches[0]));
  1173. }
  1174. }
  1175. /**
  1176. * Performs a cURL exec with the specified options after calling curlConnect().
  1177. *
  1178. * @param $curl_options
  1179. * Custom cURL options.
  1180. * @return
  1181. * Content returned from the exec.
  1182. */
  1183. protected function curlExec($curl_options) {
  1184. $this->curlInitialize();
  1185. $url = empty($curl_options[CURLOPT_URL]) ? curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL) : $curl_options[CURLOPT_URL];
  1186. if (!empty($curl_options[CURLOPT_POST])) {
  1187. // This is a fix for the Curl library to prevent Expect: 100-continue
  1188. // headers in POST requests, that may cause unexpected HTTP response
  1189. // codes from some webservers (like lighttpd that returns a 417 error
  1190. // code). It is done by setting an empty "Expect" header field that is
  1191. // not overwritten by Curl.
  1192. $curl_options[CURLOPT_HTTPHEADER][] = 'Expect:';
  1193. }
  1194. curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
  1195. // Reset headers and the session ID.
  1196. $this->session_id = NULL;
  1197. $this->headers = array();
  1198. $this->drupalSetContent(curl_exec($this->curlHandle), curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL));
  1199. $message_vars = array(
  1200. '!method' => !empty($curl_options[CURLOPT_NOBODY]) ? 'HEAD' : (empty($curl_options[CURLOPT_POSTFIELDS]) ? 'GET' : 'POST'),
  1201. '@url' => $url,
  1202. '@status' => curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE),
  1203. '!length' => format_size(strlen($this->content))
  1204. );
  1205. $message = t('!method @url returned @status (!length).', $message_vars);
  1206. $this->assertTrue($this->content !== FALSE, $message, t('Browser'));
  1207. return $this->drupalGetContent();
  1208. }
  1209. /**
  1210. * Reads headers and registers errors received from the tested site.
  1211. *
  1212. * @see _drupal_log_error().
  1213. *
  1214. * @param $curlHandler
  1215. * The cURL handler.
  1216. * @param $header
  1217. * An header.
  1218. */
  1219. protected function curlHeaderCallback($curlHandler, $header) {
  1220. $this->headers[] = $header;
  1221. // Errors are being sent via X-Drupal-Assertion-* headers,
  1222. // generated by _drupal_log_error() in the exact form required
  1223. // by DrupalWebTestCase::error().
  1224. if (preg_match('/^X-Drupal-Assertion-[0-9]+: (.*)$/', $header, $matches)) {
  1225. // Call DrupalWebTestCase::error() with the parameters from the header.
  1226. call_user_func_array(array(&$this, 'error'), unserialize(urldecode($matches[1])));
  1227. }
  1228. // Save cookies.
  1229. if (preg_match('/^Set-Cookie: ([^=]+)=(.+)/', $header, $matches)) {
  1230. $name = $matches[1];
  1231. $parts = array_map('trim', explode(';', $matches[2]));
  1232. $value = array_shift($parts);
  1233. $this->cookies[$name] = array('value' => $value, 'secure' => in_array('secure', $parts));
  1234. if ($name == $this->session_name) {
  1235. if ($value != 'deleted') {
  1236. $this->session_id = $value;
  1237. }
  1238. else {
  1239. $this->session_id = NULL;
  1240. }
  1241. }
  1242. }
  1243. // This is required by cURL.
  1244. return strlen($header);
  1245. }
  1246. /**
  1247. * Close the cURL handler and unset the handler.
  1248. */
  1249. protected function curlClose() {
  1250. if (isset($this->curlHandle)) {
  1251. curl_close($this->curlHandle);
  1252. unset($this->curlHandle);
  1253. }
  1254. }
  1255. /**
  1256. * Parse content returned from curlExec using DOM and SimpleXML.
  1257. *
  1258. * @return
  1259. * A SimpleXMLElement or FALSE on failure.
  1260. */
  1261. protected function parse() {
  1262. if (!$this->elements) {
  1263. // DOM can load HTML soup. But, HTML soup can throw warnings, suppress
  1264. // them.
  1265. @$htmlDom = DOMDocument::loadHTML($this->content);
  1266. if ($htmlDom) {
  1267. $this->pass(t('Valid HTML found on "@path"', array('@path' => $this->getUrl())), t('Browser'));
  1268. // It's much easier to work with simplexml than DOM, luckily enough
  1269. // we can just simply import our DOM tree.
  1270. $this->elements = simplexml_import_dom($htmlDom);
  1271. }
  1272. }
  1273. if (!$this->elements) {
  1274. $this->fail(t('Parsed page successfully.'), t('Browser'));
  1275. }
  1276. return $this->elements;
  1277. }
  1278. /**
  1279. * Retrieves a Drupal path or an absolute path.
  1280. *
  1281. * @param $path
  1282. * Drupal path or URL to load into internal browser
  1283. * @param $options
  1284. * Options to be forwarded to url().
  1285. * @param $headers
  1286. * An array containing additional HTTP request headers, each formatted as
  1287. * "name: value".
  1288. * @return
  1289. * The retrieved HTML string, also available as $this->drupalGetContent()
  1290. */
  1291. protected function drupalGet($path, array $options = array(), array $headers = array()) {
  1292. $options['absolute'] = TRUE;
  1293. // We re-using a CURL connection here. If that connection still has certain
  1294. // options set, it might change the GET into a POST. Make sure we clear out
  1295. // previous options.
  1296. $out = $this->curlExec(array(CURLOPT_HTTPGET => TRUE, CURLOPT_URL => url($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => $headers));
  1297. $this->refreshVariables(); // Ensure that any changes to variables in the other thread are picked up.
  1298. // Replace original page output with new output from redirected page(s).
  1299. if ($new = $this->checkForMetaRefresh()) {
  1300. $out = $new;
  1301. }
  1302. $this->verbose('GET request to: ' . $path .
  1303. '<hr />Ending URL: ' . $this->getUrl() .
  1304. '<hr />' . $out);
  1305. return $out;
  1306. }
  1307. /**
  1308. * Retrieve a Drupal path or an absolute path and JSON decode the result.
  1309. */
  1310. protected function drupalGetAJAX($path, array $options = array(), array $headers = array()) {
  1311. return drupal_json_decode($this->drupalGet($path, $options, $headers));
  1312. }
  1313. /**
  1314. * Execute a POST request on a Drupal page.
  1315. * It will be done as usual POST request with SimpleBrowser.
  1316. *
  1317. * @param $path
  1318. * Location of the post form. Either a Drupal path or an absolute path or
  1319. * NULL to post to the current page. For multi-stage forms you can set the
  1320. * path to NULL and have it post to the last received page. Example:
  1321. *
  1322. * @code
  1323. * // First step in form.
  1324. * $edit = array(...);
  1325. * $this->drupalPost('some_url', $edit, t('Save'));
  1326. *
  1327. * // Second step in form.
  1328. * $edit = array(...);
  1329. * $this->drupalPost(NULL, $edit, t('Save'));
  1330. * @endcode
  1331. * @param $edit
  1332. * Field data in an associative array. Changes the current input fields
  1333. * (where possible) to the values indicated. A checkbox can be set to
  1334. * TRUE to be checked and FALSE to be unchecked. Note that when a form
  1335. * contains file upload fields, other fields cannot start with the '@'
  1336. * character.
  1337. *
  1338. * Multiple select fields can be set using name[] and setting each of the
  1339. * possible values. Example:
  1340. * @code
  1341. * $edit = array();
  1342. * $edit['name[]'] = array('value1', 'value2');
  1343. * @endcode
  1344. * @param $submit
  1345. * Value of the submit button whose click is to be emulated. For example,
  1346. * t('Save'). The processing of the request depends on this value. For

Large files files are truncated, but you can click here to view the full file