PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tests/kernel/classes/clusterfilehandlers/dfsbackends/dfs_test.php

http://github.com/ezsystems/ezpublish
PHP | 211 lines | 149 code | 43 blank | 19 comment | 9 complexity | 0bcdf60b800d11bf80d0f225cf2804e4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the eZDFSFileHandlerDFSBackendTest class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package tests
  9. */
  10. class eZDFSFileHandlerDFSBackendTest extends ezpTestCase
  11. {
  12. public function setUp()
  13. {
  14. if ( !file_exists( $this->path ) )
  15. {
  16. mkdir( $this->path );
  17. }
  18. $ini = eZINI::instance( 'file.ini' );
  19. $ini->setVariable( 'eZDFSClusteringSettings', 'MountPointPath', $this->path );
  20. $this->dfsbackend = new eZDFSFileHandlerDFSBackend();
  21. }
  22. /**
  23. * Tests if $path exists and that its contents matches $content
  24. */
  25. protected function localFileIsValid( $path, $content = 'testcontent' )
  26. {
  27. return ( file_exists( $path ) && ( file_get_contents( $path ) == $content ) );
  28. }
  29. /**
  30. * Tests if local file $path exists on DFS and that its contents matches
  31. * $content
  32. */
  33. protected function DFSFileIsValid( $path, $content = 'testcontent' )
  34. {
  35. $path = $this->makeDFSPath( $path );
  36. return ( file_exists( $path ) && ( file_get_contents( $path ) == $content ) );
  37. }
  38. protected function createDFSFile( $path, $content = 'testcontent' )
  39. {
  40. return $this->createFile( $this->makeDFSPath( $path ), $content );
  41. }
  42. protected function createLocalFile( $path, $content = 'testcontent' )
  43. {
  44. return $this->createFile( $path, $content );
  45. }
  46. protected function createFile( $path, $content )
  47. {
  48. return eZFile::create( basename( $path ), dirname( $path ), $content, false );
  49. }
  50. protected function makeDFSPath( $path )
  51. {
  52. return $this->path . $path;
  53. }
  54. public function testCopyFromDFSToDFS()
  55. {
  56. $srcFile = 'testCopyFromDFSToDFS';
  57. $tgtFile = 'testCopyFromDFSToDFS_copy';
  58. $this->createDFSFile( $srcFile );
  59. $this->dfsbackend->copyFromDFSToDFS( $srcFile, $tgtFile );
  60. $this->assertTrue( $this->DFSFileIsValid( $srcFile ), "Source file DFS://$srcFile no longer exists" );
  61. $this->assertTrue( $this->DFSFileIsValid( $tgtFile ), "Target file DFS://$tgtFile doesn't exist" );
  62. }
  63. public function testDeleteSingleFile()
  64. {
  65. $file = 'testDeleteSingleFile';
  66. $this->createDFSFile( $file );
  67. if ( !$this->DFSFileIsValid( $file ) )
  68. {
  69. $this->fail( "Test file DFS://$file was not created correctly" );
  70. }
  71. $this->dfsbackend->delete( $file );
  72. $this->assertFalse( $this->DFSFileIsValid( $file ), "Test file DFS://$file is still valid" );
  73. }
  74. public function testDeleteMultipleFiles()
  75. {
  76. for ( $i = 0, $files = array(); $i <= 10; $i++ )
  77. {
  78. $file = 'testDeleteSingleFile' . uniqid();
  79. $this->createDFSFile( $file );
  80. if ( !$this->DFSFileIsValid( $file ) )
  81. {
  82. $this->fail( "Test file DFS://$file was not created correctly" );
  83. }
  84. $files[] = $file;
  85. }
  86. $this->dfsbackend->delete( $files );
  87. foreach ( $files as $file )
  88. {
  89. $this->assertFalse( $this->DFSFileIsValid( $file ), "Test file DFS://$file is still valid" );
  90. }
  91. }
  92. public function testCopyFromDFS()
  93. {
  94. $file = 'var/testCopyFromDFSToFS';
  95. $this->createDFSFile( $file );
  96. $this->dfsbackend->copyFromDFS( $file );
  97. $this->assertTrue( $this->localFileIsValid( $file ), "Test file FS://$file is not valid" );
  98. }
  99. public function testCopyFromDFSWithNewName()
  100. {
  101. $srcFile = 'testCopyFromDFSToFS';
  102. $tgtFile = 'var/somesubfolder/' . $srcFile . '_localcopy';
  103. $this->createDFSFile( $srcFile );
  104. $this->dfsbackend->copyFromDFS( $srcFile, $tgtFile );
  105. $this->assertTrue( $this->localFileIsValid( $tgtFile ), "Test file FS://$tgtFile is not valid" );
  106. }
  107. public function testCopyToDFS()
  108. {
  109. $srcFile = 'var/testCopyToDFS';
  110. $this->createLocalFile( $srcFile );
  111. $this->dfsbackend->copyToDFS( $srcFile );
  112. $this->assertTrue( $this->DFSFileIsValid( $srcFile ), "DFS://$srcFile isn't valid" );
  113. }
  114. public function testCopyToDFSWithNewName()
  115. {
  116. $srcFile = 'var/testCopyToDFS';
  117. $dstFile = 'var/subfolder/testCopyToDFS_copy';
  118. $this->createLocalFile( $srcFile );
  119. $this->dfsbackend->copyToDFS( $srcFile, $dstFile );
  120. $this->assertTrue( $this->DFSFileIsValid( $dstFile ), "DFS://$dstFile isn't valid" );
  121. }
  122. public function testGetContents()
  123. {
  124. $file = 'testGetContents';
  125. $contents = md5( uniqid() );
  126. $this->createDFSFile( $file, $contents );
  127. $dfsContents = $this->dfsbackend->getContents( $file );
  128. $this->assertEquals( $dfsContents, $contents, "DFS://$file contents doesn't match the expected contents" );
  129. }
  130. public function testPassthrough()
  131. {
  132. $file = 'testPassthrough';
  133. $contents = '';
  134. for ( $i = 0; $i < 1000; $i++ )
  135. {
  136. $contents .= md5( uniqid() );
  137. }
  138. $this->createDFSFile( $file, $contents );
  139. ob_start();
  140. $this->dfsbackend->passthrough( $file );
  141. $passedContents = ob_get_contents();
  142. ob_end_clean();
  143. $this->assertEquals( $passedContents, $contents, "Contents passed from DFS://$file doesn't match the expected contents" );
  144. }
  145. public function testCreateFileOnDFS()
  146. {
  147. $filePath = 'var/testCreateFileOnDFS';
  148. $contents = md5( uniqid() );
  149. $this->dfsbackend->createFileOnDFS( $filePath, $contents );
  150. $this->assertTrue( $this->DFSFileIsValid( $filePath, $contents ), "DFS://$filePath is not valid" );
  151. }
  152. public function testRenameOnDFS()
  153. {
  154. $oldFile = 'var/testRenameOnDFS';
  155. $newFile = 'var/testRenameOnDFS_renamed';
  156. $this->createDFSFile( $oldFile );
  157. $this->dfsbackend->renameOnDFS( $oldFile, $newFile );
  158. $this->assertTrue( $this->DFSFileIsValid( $newFile ), "DFS://$newFile doesn't exist" );
  159. $this->assertFalse( $this->DFSFileIsValid( $oldFile ), "DFS://$oldFile still exists" );
  160. }
  161. /**
  162. * DFSBackend instance
  163. * @var eZDFSFileHandlerDFSBackend
  164. */
  165. protected $dfsbackend;
  166. protected $path = 'var/testdfsmount/';
  167. }
  168. ?>