RoundCube Webmail
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.2 KiB

  1. <?php
  2. namespace Roundcube\Tests;
  3. use Dom\HTMLDocument;
  4. use Dom\NodeList;
  5. use Dom\XPath;
  6. use Masterminds\HTML5;
  7. /*
  8. +-----------------------------------------------------------------------+
  9. | This file is part of the Roundcube Webmail client |
  10. | |
  11. | Copyright (C) The Roundcube Dev Team |
  12. | |
  13. | Licensed under the GNU General Public License version 3 or |
  14. | any later version with exceptions for skins & plugins. |
  15. | See the README file for a full license statement. |
  16. | |
  17. | PURPOSE: |
  18. | Environment initialization script for unit tests |
  19. +-----------------------------------------------------------------------+
  20. | Author: Thomas Bruederli <roundcube@gmail.com> |
  21. | Author: Aleksander Machniak <alec@alec.pl> |
  22. +-----------------------------------------------------------------------+
  23. */
  24. error_reporting(\E_ALL);
  25. if (\PHP_SAPI != 'cli') {
  26. exit('Not in shell mode (php-cli)');
  27. }
  28. if (!defined('INSTALL_PATH')) {
  29. define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/');
  30. }
  31. define('ROUNDCUBE_TEST_MODE', true);
  32. define('ROUNDCUBE_TEST_SESSION', microtime(true));
  33. define('TESTS_DIR', __DIR__ . '/');
  34. if (@is_dir(TESTS_DIR . 'config')) {
  35. define('RCUBE_CONFIG_DIR', TESTS_DIR . 'config');
  36. }
  37. // Some tests depend on the way phpunit is executed
  38. $_SERVER['SCRIPT_NAME'] = 'vendor/bin/phpunit';
  39. require_once INSTALL_PATH . 'program/include/iniset.php';
  40. \rcmail::get_instance(0, 'test')->config->set('devel_mode', false);
  41. // Initialize database and environment
  42. ActionTestCase::init();
  43. /**
  44. * Call protected/private method of a object.
  45. *
  46. * @param object $object Object instance
  47. * @param string $method Method name to call
  48. * @param array $parameters array of parameters to pass into method
  49. * @param string $class Object class
  50. *
  51. * @return mixed method return
  52. */
  53. function invokeMethod($object, $method, array $parameters = [], $class = null)
  54. {
  55. $reflection = new \ReflectionClass($class ?: get_class($object));
  56. $method = $reflection->getMethod($method);
  57. $method->setAccessible(true);
  58. return $method->invokeArgs($object, $parameters);
  59. }
  60. /**
  61. * Get value of a protected/private property of a object.
  62. *
  63. * @param \rcube_sieve_vacation $object Object
  64. * @param string $name Property name
  65. * @param string $class Object class
  66. *
  67. * @return mixed Property value
  68. */
  69. function getProperty($object, $name, $class = null)
  70. {
  71. $reflection = new \ReflectionClass($class ?: get_class($object));
  72. $property = $reflection->getProperty($name);
  73. $property->setAccessible(true);
  74. return $property->getValue($object);
  75. }
  76. /**
  77. * Set protected/private property of a object.
  78. *
  79. * @param \rcube_sieve_vacation $object Object
  80. * @param string $name Property name
  81. * @param mixed $value Property value
  82. * @param string $class Object class
  83. */
  84. function setProperty($object, $name, $value, $class = null): void
  85. {
  86. $reflection = new \ReflectionClass($class ?: get_class($object));
  87. $property = $reflection->getProperty($name);
  88. $property->setAccessible(true);
  89. $property->setValue($object, $value);
  90. }
  91. /**
  92. * Parse HTML content and extract nodes by XPath query
  93. *
  94. * @param string $html HTML content
  95. * @param string $xpath_query XPath query
  96. *
  97. * @return \DOMNodeList|NodeList List of nodes found
  98. */
  99. function getHTMLNodes($html, $xpath_query)
  100. {
  101. // Try HTML5 parser available in PHP >= 8.4
  102. if (class_exists(HTMLDocument::class)) {
  103. $options = constant('Dom\HTML_NO_DEFAULT_NS') | \LIBXML_COMPACT | \LIBXML_NOERROR;
  104. $doc = HTMLDocument::createFromString($html, $options, RCUBE_CHARSET);
  105. $xpath = new XPath($doc);
  106. } else {
  107. $html5 = new HTML5(['disable_html_ns' => true]);
  108. $doc = $html5->loadHTML($html);
  109. $xpath = new \DOMXPath($doc);
  110. }
  111. return $xpath->query($xpath_query);
  112. }