PageRenderTime 54ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/tests/kernel/private/webdav/backend_content_regression_test.php

http://github.com/ezsystems/ezpublish
PHP | 369 lines | 168 code | 53 blank | 148 comment | 9 complexity | f9133d1ce0d38e15db0256105ba9dfe0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the eZWebDAVBackendContentRegressionTest class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package tests
  9. */
  10. require_once( 'wrappers.php' );
  11. /**
  12. * Main test class for WebDAV tests.
  13. *
  14. * Read doc/features/specifications/trunk/webdav/testing.txt for information
  15. * about the WebDAV tests and what is needed to make them work on another
  16. * machine.
  17. *
  18. * This class requires the extension eZSiteAccessHelper
  19. * {@link http://svn.ez.no/svn/commercial/projects/qa/trunk/ezsiteaccesshelper/}.
  20. *
  21. * In the constructor:
  22. * - ./siteaccess/site.ini.append.php.replace is copied over site.ini.append.php,
  23. * with certain values replaced dynamically (@ezc_siteaccess@ and
  24. * @ezc_webdav_database@)
  25. * - the extension ezsiteaccesshelper is called to create the siteaccess with
  26. * the same name as this class (in lowercase), and it will copy the site.ini.append.php
  27. * file to the ezp/siteaccess/@ezc_siteaccess@ folder
  28. * - the extension ezsiteaccesshelper is called to enable the created siteaccess
  29. * in ezp/siteaccess/override/site.ini.append.php
  30. */
  31. class eZWebDAVBackendContentRegressionTest extends ezpDatabaseRegressionTest
  32. {
  33. /**
  34. * Setting needed to keep the global variables working between the tests.
  35. *
  36. * @var bool
  37. */
  38. protected $backupGlobals = FALSE;
  39. /**
  40. * Setup webdav test siteaccess & fills $this->files with all the
  41. * .request files found in the regression directory, recursively.
  42. */
  43. public function __construct()
  44. {
  45. $siteaccess = strtolower( __CLASS__ );
  46. // List of values that should be placed in temporary site.ini file for webdav testing
  47. $replace = array();
  48. $dsn = ezpTestRunner::dsn();
  49. $replace['@ezc_siteaccess@'] = $siteaccess;
  50. $replace['@ezc_db_phptype@'] = $dsn->phptype;
  51. $replace['@ezc_db_server@'] = $dsn->server;
  52. $replace['@ezc_db_port@'] = $dsn->port ? $dsn->port : '';
  53. $replace['@ezc_db_user@'] = $dsn->user;
  54. $replace['@ezc_db_password@'] = $dsn->password;
  55. $replace['@ezc_db_database@'] = $dsn->database;
  56. $replace['@ezc_db_socket@'] = $dsn->socket ? 'enabled' : 'disabled';
  57. // replace $replace values in site.ini.append.php.replace
  58. $contents = file_get_contents( dirname( __FILE__ ) . '/siteaccess/site.ini.append.php.replace' );
  59. foreach ( $replace as $key => $replacement )
  60. {
  61. $contents = str_replace( $key, $replacement, $contents );
  62. }
  63. // create siteaccess folder
  64. if ( !file_exists( 'settings/siteaccess/' . $siteaccess ) )
  65. {
  66. mkdir( 'settings/siteaccess/' . $siteaccess );
  67. }
  68. // store site.ini.append.php file
  69. file_put_contents( 'settings/siteaccess/' . $siteaccess . '/site.ini.append.php', $contents );
  70. // setup ini system to read siteaccess
  71. $ini = eZINI::instance();
  72. $ini->prependOverrideDir( 'siteaccess/' . $siteaccess, false, 'siteaccess', 'siteaccess' );
  73. $ini->loadCache();
  74. // load tests
  75. $this->readDirRecursively( dirname( __FILE__ ) . '/regression', $this->files, 'request' );
  76. parent::__construct();
  77. }
  78. /**
  79. * Called by PHPUnit before each test.
  80. */
  81. public function setUp()
  82. {
  83. // Call the setUp() in ezpDatabaseTestCase
  84. parent::setUp();
  85. // Set these to your own values
  86. // @todo get these values automatically from somewhere (how to get the password?)
  87. $GLOBALS['ezc_webdav_username'] = 'admin';
  88. $GLOBALS['ezc_webdav_password'] = 'publish';
  89. $GLOBALS['ezc_webdav_host'] = 'webdav.ezp';
  90. // A compound value from the above
  91. $GLOBALS['ezc_webdav_url'] = 'http://' . $GLOBALS['ezc_webdav_host'] . '/';
  92. // Set some server variables (not all of them are needed)
  93. $_SERVER['HTTP_USER_AGENT'] = 'cadaver/0.22.5 neon/0.26.3';
  94. $_SERVER['SERVER_NAME'] = $GLOBALS['ezc_webdav_host'];
  95. $_SERVER['SERVER_PORT'] = '80';
  96. // Set to null various variables used in the tests
  97. $GLOBALS['ezc_response_body'] = null;
  98. $GLOBALS['ezc_post_body'] = null;
  99. $GLOBALS['ezc_webdav_testfolder'] = null;
  100. $GLOBALS['ezc_webdav_testfolderobject'] = null;
  101. $GLOBALS['ezc_webdav_testfolderid'] = null;
  102. // Not sure if these 2 values are needed
  103. $_SERVER['SCRIPT_FILENAME'] = eZSys::rootDir() . DIRECTORY_SEPARATOR . 'index.php';
  104. $_SERVER['DOCUMENT_ROOT'] = eZSys::rootDir();
  105. $GLOBALS['ezc_siteaccess'] = strtolower( __CLASS__ );
  106. }
  107. /**
  108. * Called by PHPUnit after each test.
  109. */
  110. public function tearDown()
  111. {
  112. // Remove the created folder if it exists
  113. if ( $GLOBALS['ezc_webdav_testfolderobject'] !== null )
  114. {
  115. $GLOBALS['ezc_webdav_testfolderobject']->remove();
  116. $GLOBALS['ezc_webdav_testfolderobject'] = null;
  117. }
  118. /*$siteaccess = strtolower( __CLASS__ );
  119. //remove siteaccess site.ini file & directory
  120. if ( file_exists( 'settings/siteaccess/' . $siteaccess . '/site.ini.append.php' ) )
  121. unlink( 'settings/siteaccess/' . $siteaccess . '/site.ini.append.php' );
  122. if ( file_exists( 'settings/siteaccess/' . $siteaccess ) )
  123. rmdir( 'settings/siteaccess/' . $siteaccess );
  124. // refresh ini instance
  125. $ini = eZINI::instance();
  126. $ini->removeOverrideDir( 'siteaccess', 'siteaccess' );
  127. $ini->loadCache();*/
  128. parent::tearDown();
  129. }
  130. /**
  131. * Called by PHPUnit to create this test suite.
  132. */
  133. public static function suite()
  134. {
  135. $ini = eZINI::instance( 'i18n.ini' );
  136. list( $i18nSettings['internal-charset'], $i18nSettings['http-charset'], $i18nSettings['mbstring-extension'] ) =
  137. $ini->variableMulti( 'CharacterSettings', array( 'Charset', 'HTTPCharset', 'MBStringExtension' ), array( false, false, 'enabled' ) );
  138. eZTextCodec::updateSettings( $i18nSettings );
  139. return new ezpTestRegressionSuite( __CLASS__ );
  140. }
  141. /**
  142. * Clean the $text of dynamic stuff (creation date, etags, etc).
  143. *
  144. * Optionally append $body to $text, with "\n\n" between them.
  145. *
  146. * @param string $text
  147. * @param string $body
  148. * @return string
  149. */
  150. protected function cleanForCompare( $text, $body = null )
  151. {
  152. $result = $text;
  153. $result = preg_replace( array( '/ETag: (\w+)/' ), array( 'ETag: XXX' ), $result );
  154. $result = preg_replace( array( '/Server: ([\w\/\.]+)/' ), array( 'Server: XXX' ), $result );
  155. $result = preg_replace( '@<D:creationdate>.*?</D:creationdate>@', '<D:creationdate>XXX</D:creationdate>', $result );
  156. $result = preg_replace( '@<D:getlastmodified>.*?</D:getlastmodified>@', '<D:getlastmodified>XXX</D:getlastmodified>', $result );
  157. $result = preg_replace( '@<D:getetag>.*?</D:getetag>@', '<D:getetag>XXX</D:getetag>', $result );
  158. $result = preg_replace( '/@ezc_webdav_host@/', $GLOBALS['ezc_webdav_host'], $result );
  159. $result = preg_replace( '/@ezc_siteaccess@/', $GLOBALS['ezc_siteaccess'], $result );
  160. $result = preg_replace( '/@ezc_webdav_testfolder@/', $GLOBALS['ezc_webdav_testfolder'], $result );
  161. if ( $body !== null )
  162. {
  163. $result = "{$result}\n\n{$body}";
  164. }
  165. return $result;
  166. }
  167. /**
  168. * Skips the test $file if the directory name is uncommented in $skipTests
  169. * inside the function.
  170. *
  171. * @param string $file
  172. */
  173. protected function skip( $file )
  174. {
  175. // uncomment the tests that you want to skip
  176. $skipTests = array(
  177. // 'COPY',
  178. // 'DELETE',
  179. // 'GET',
  180. // 'HEAD',
  181. // 'MKCOL',
  182. // 'MOVE',
  183. // 'OPTIONS',
  184. // 'PROPFIND',
  185. // 'PROPPATCH',
  186. // 'PUT',
  187. );
  188. foreach ( $skipTests as $test )
  189. {
  190. if ( strpos( $file, $test ) !== false )
  191. {
  192. $this->markTestSkipped( "Test environment is configured to skip {$test} tests." );
  193. break;
  194. }
  195. }
  196. }
  197. /**
  198. * Runs the $file (.request file from $this->files) as a PHPUnit test.
  199. *
  200. * Steps performed:
  201. * - setUp() is called automatically before this function
  202. * - skip the test $file if declared. See {@link skip()}
  203. * - identify test files attached to the .request file $file (.expected, .body)
  204. * - initialize various global variables (together with $GLOBALS from setUp())
  205. * - create an eZ Publish folder with the name $file
  206. * - include $file (the .request file) as PHP code. $GLOBALS and $_SERVER values set in there
  207. * will be used further on
  208. * - initialize the WebDAV system like in webdav.php (it does NOT go through webdav.php)
  209. * - create an eZWebDAVContentBackend object and use ezcWebdavServer to handle the
  210. * WebDAV request specified in the .request file $file (through $_SERVER variables
  211. * 'REQUEST_METHOD' and 'REQUEST_URI' and others.
  212. * - the output from the WebDAV system is collected in $GLOBALS['ezc_response_body']
  213. * (through hacks in wrappers.php).
  214. * - append the .body file contents to the $GLOBALS['ezc_response_body'] if it exists
  215. * - clean the response and the .expected file with cleanForCompare() (eg. creation date, etags etc)
  216. * - compare the response and the .expected file with assertEquals(). Same contents means the test passed.
  217. * - tearDown() is called automatically after this function
  218. *
  219. * See doc/specifications/trunk/webdav/testing.txt for detailed information
  220. * about each $GLOBALS and $_SERVER variables.
  221. *
  222. * @param string $file
  223. */
  224. public function testRunRegression( $file )
  225. {
  226. // 'ezc' = use eZWebDAVContentBackend (new eZ Publish WebDAV based on ezcWebdav)
  227. // 'ezp' = use eZWebDAVContentServer (old eZ Publish WebDAV)
  228. // Only 'ezc' is supported for now
  229. $system = 'ezc';
  230. // uncomment the tests that you want to skip in the skip() function
  231. $this->skip( $file );
  232. $error = '';
  233. $response = null;
  234. $outFile = $this->outFileName( $file, '.request', '.expected' );
  235. $bodyFile = $this->outFileName( $file, '.request', '.body' );
  236. $parts = pathinfo( $file );
  237. $GLOBALS['ezc_webdav_testfolder'] = $parts['filename'];
  238. // Create an eZ Publish folder for each test with the name of the test
  239. // $GLOBALS['ezc_webdav_testfolderid'] can be used in the .request file
  240. // to create file, image, folder etc under the test folder.
  241. $folder = new ezpObject( "folder", 2 );
  242. $folder->name = $GLOBALS['ezc_webdav_testfolder'];
  243. $folder->publish();
  244. $GLOBALS['ezc_webdav_testfolderobject'] = $folder;
  245. $GLOBALS['ezc_webdav_testfolderid'] = $folder->mainNode->node_id;
  246. // var_dump( $GLOBALS['ezc_webdav_testfolder'] . ' (' . $GLOBALS['ezc_webdav_testfolderid'] . ')' );
  247. //eZExtension::activateExtensions( 'default' );
  248. //eZModule::setGlobalPathList( array( "kernel" ) );
  249. eZUser::logoutCurrent();
  250. eZSys::init( 'webdav.php' );
  251. // include the .request file. $GLOBALS and $_SERVER defined in the file
  252. // will be used in the test
  253. include $file;
  254. // var_dump( '--- After include' );
  255. // These values can be overwritten in the included $file which contains the WebDAV request
  256. $username = $GLOBALS['ezc_webdav_username'];
  257. $password = $GLOBALS['ezc_webdav_password'];
  258. // Set the HTTP_AUTHORIZATION header
  259. $_SERVER['HTTP_AUTHORIZATION'] = 'Basic ' . base64_encode( "{$username}:{$password}" );
  260. // var_dump( 'Default REQUEST_URI: ' . $_SERVER['REQUEST_URI'] );
  261. if ( $system === 'ezc' )
  262. {
  263. // Use eZ Components
  264. // clean the REQUEST_URI and HTTP_DESTINATION
  265. $_SERVER['REQUEST_URI'] = urldecode( $_SERVER['REQUEST_URI'] );
  266. if ( isset( $_SERVER['HTTP_DESTINATION'] ) )
  267. {
  268. $_SERVER['HTTP_DESTINATION'] = urldecode( $_SERVER['HTTP_DESTINATION'] );
  269. }
  270. $server = ezcWebdavServer::getInstance();
  271. $backend = new eZWebDAVContentBackend();
  272. $server->configurations = new ezcWebdavServerConfigurationManagerWrapper();
  273. $server->init( new ezcWebdavBasicPathFactory( $GLOBALS['ezc_webdav_url'] ),
  274. new ezcWebdavXmlTool(),
  275. new ezcWebdavPropertyHandler(),
  276. new ezcWebdavHeaderHandler(),
  277. new ezcWebdavTransportWrapper() );
  278. $server->auth = new eZWebDAVContentBackendAuth();
  279. }
  280. else
  281. {
  282. // Use the previous WebDAV system in eZ Publish
  283. $backend = new eZWebDAVContentServerWrapper();
  284. }
  285. $currentSite = $backend->currentSiteFromPath( $_SERVER['REQUEST_URI'] );
  286. if ( $currentSite )
  287. {
  288. $backend->setCurrentSite( $currentSite );
  289. }
  290. if ( $system === 'ezc' )
  291. {
  292. $server->handle( $backend );
  293. }
  294. else
  295. {
  296. $backend->processClientRequest();
  297. }
  298. // This value comes from the included $file which contains the WebDAV request
  299. $response = trim( $GLOBALS['ezc_response_body'] );
  300. $expected = trim( file_get_contents( $outFile ) );
  301. $body = null;
  302. if ( file_exists( $bodyFile ) )
  303. {
  304. $body = trim( file_get_contents( $bodyFile ) );
  305. }
  306. // replace dynamic text (eg. ETag) with static text
  307. // $body = optional body content (eg. for binary files) to be appended to $expected
  308. $expected = $this->cleanForCompare( $expected, $body );
  309. $response = $this->cleanForCompare( $response );
  310. $this->assertEquals( $expected, $response, "The expected response " . basename( $outFile ) . " (" . strlen( $expected ) . ") is not the same as the response got from request " . basename( $file ) . " (" . strlen( $response ) . "). {$error}" );
  311. }
  312. }
  313. ?>