/Template/tests/source_code_test.php

https://github.com/F5/zetacomponents · PHP · 452 lines · 281 code · 61 blank · 110 comment · 29 complexity · 6a4b7e7ed8c413ae7b88114f4e775dce MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  22. * @version //autogentag//
  23. * @filesource
  24. * @package Template
  25. * @subpackage Tests
  26. */
  27. /**
  28. * @package Template
  29. * @subpackage Tests
  30. */
  31. class ezcTemplateSourceCodeTest extends ezcTestCase
  32. {
  33. public static function suite()
  34. {
  35. return new PHPUnit_Framework_TestSuite( "ezcTemplateSourceCodeTest" );
  36. }
  37. protected function setUp()
  38. {
  39. $this->basePath = realpath( dirname( __FILE__ ) ) . '/';
  40. $this->templatePath = $this->basePath . 'templates/';
  41. $this->templateStorePath = $this->basePath . 'stored_templates/';
  42. // remove temporarily stored file if possible
  43. if ( file_exists( $this->templateStorePath . "zhadum.ezt" ) )
  44. unlink( $this->templateStorePath . "zhadum.ezt" );
  45. if ( file_exists( $this->templateStorePath . "zhadum.ezt~" ) )
  46. unlink( $this->templateStorePath . "zhadum.ezt~" );
  47. if ( file_exists( $this->templateStorePath . "#zhadum.ezt#" ) )
  48. unlink( $this->templateStorePath . "#zhadum.ezt#" );
  49. }
  50. protected function tearDown()
  51. {
  52. // remove temporarily stored file if possible
  53. if ( file_exists( $this->templateStorePath . "zhadum.ezt" ) )
  54. unlink( $this->templateStorePath . "zhadum.ezt" );
  55. if ( file_exists( $this->templateStorePath . "zhadum.ezt~" ) )
  56. unlink( $this->templateStorePath . "zhadum.ezt~" );
  57. if ( file_exists( $this->templateStorePath . "#zhadum.ezt#" ) )
  58. unlink( $this->templateStorePath . "#zhadum.ezt#" );
  59. }
  60. /**
  61. * Test default constructor values
  62. */
  63. public function testDefault()
  64. {
  65. $src = new ezcTemplateSourceCode( $this->templatePath . "zhadum.ezt" );
  66. self::assertSame( $this->templatePath . "zhadum.ezt", $src->stream, 'Property <stream> does not return correct value.' );
  67. self::assertSame( false, $src->resource, 'Property <resource> does not return correct value.' );
  68. self::assertSame( false, $src->code, 'Property <code> does not return correct value.' );
  69. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  70. }
  71. /**
  72. * Test passing constructor values
  73. */
  74. public function testInit()
  75. {
  76. $src = new ezcTemplateSourceCode( $this->templatePath . "zhadum.ezt",
  77. "planet:zhadum.ezt", "you are hereby warned" );
  78. self::assertSame( $this->templatePath . "zhadum.ezt", $src->stream, 'Property <stream> does not return correct value.' );
  79. self::assertSame( "planet:zhadum.ezt", $src->resource, 'Property <resource> does not return correct value.' );
  80. self::assertSame( "you are hereby warned", $src->code, 'Property <code> does not return correct value.' );
  81. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  82. }
  83. /**
  84. * Test hasCode() with code initialised
  85. */
  86. public function testHasCode()
  87. {
  88. $src = new ezcTemplateSourceCode( $this->templatePath . "zhadum.ezt",
  89. "planet:zhadum.ezt", "you are hereby warned" );
  90. self::assertSame( $this->templatePath . "zhadum.ezt", $src->stream, 'Property <stream> does not return correct value.' );
  91. self::assertSame( "planet:zhadum.ezt", $src->resource, 'Property <resource> does not return correct value.' );
  92. self::assertSame( "you are hereby warned", $src->code, 'Property <code> does not return correct value.' );
  93. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  94. self::assertSame( true, $src->hasCode(), 'Method hasCode() does not return true.' );
  95. }
  96. /**
  97. * Test hasCode() with no code
  98. */
  99. public function testHasNoCode()
  100. {
  101. $src = new ezcTemplateSourceCode( $this->templatePath . "zhadum.ezt",
  102. "planet:zhadum.ezt", false );
  103. self::assertSame( $this->templatePath . "zhadum.ezt", $src->stream, 'Property <stream> does not return correct value.' );
  104. self::assertSame( "planet:zhadum.ezt", $src->resource, 'Property <resource> does not return correct value.' );
  105. self::assertSame( false, $src->code, 'Property <code> does not return correct value.' );
  106. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  107. self::assertSame( false, $src->hasCode(), 'Method hasCode() does not return false.' );
  108. }
  109. /**
  110. * Test isAvailable()
  111. */
  112. public function testIsAvailable()
  113. {
  114. $src = new ezcTemplateSourceCode( $this->templatePath . "zhadum.ezt",
  115. "planet:zhadum.ezt", false );
  116. self::assertTrue( file_exists( $this->templatePath . "zhadum.ezt" ), 'Template file does not exist on filesystem' );
  117. self::assertSame( true, $src->isAvailable(), 'Template file is not available while the file exists' );
  118. }
  119. /**
  120. * Test isAvailable()
  121. */
  122. public function testIsNotAvailable()
  123. {
  124. $src = new ezcTemplateSourceCode( $this->templatePath . "zhadum_no_such_file.ezt",
  125. "planet:zhadum.ezt", false );
  126. self::assertTrue( !file_exists( $this->templatePath . "zhadum_no_such_file.ezt" ), 'Template file exist on filesystem, should not be present' );
  127. self::assertSame( false, $src->isAvailable(), 'Template file is available while the file does not exists' );
  128. }
  129. /**
  130. * Test isReadable() with readable file
  131. */
  132. public function testIsReadable()
  133. {
  134. $src = new ezcTemplateSourceCode( $this->templatePath . "zhadum.ezt",
  135. "planet:zhadum.ezt", false );
  136. self::assertTrue( file_exists( $this->templatePath . "zhadum.ezt" ), 'Template file does not exist on filesystem' );
  137. self::assertTrue( is_readable( $this->templatePath . "zhadum.ezt" ), 'Template file cannot be read' );
  138. self::assertSame( true, $src->isReadable(), 'Template file is not available while the file exists' );
  139. }
  140. /**
  141. * Test isReadable() with unreadable file
  142. */
  143. public function testIsNotReadable()
  144. {
  145. // If running as root you can always write, so this test should be
  146. // skipped when running as root.
  147. if ( !ezcBaseFeatures::hasFunction("posix_getuid") || posix_getuid() == 0 )
  148. {
  149. return;
  150. }
  151. copy( $this->templatePath . "zhadum.ezt",
  152. $this->templateStorePath . "zhadum.ezt" );
  153. // This only works on Linux/Unix, what to do here on other platforms?
  154. $old = umask( 0 );
  155. chmod( $this->templateStorePath . "zhadum.ezt", 0222 );
  156. umask( $old );
  157. $src = new ezcTemplateSourceCode( $this->templateStorePath . "zhadum.ezt",
  158. "planet:zhadum.ezt", false );
  159. self::assertTrue( file_exists( $this->templateStorePath . "zhadum.ezt" ), 'Template file does not exist on filesystem' );
  160. self::assertTrue( !is_readable( $this->templateStorePath . "zhadum.ezt" ), 'Template file is still readable' );
  161. self::assertSame( false, $src->isReadable(), 'Unreadable template file is considered readable' );
  162. }
  163. /**
  164. * Test isWriteable() with writeable file
  165. */
  166. public function testIsWriteable()
  167. {
  168. copy( $this->templatePath . "zhadum.ezt",
  169. $this->templateStorePath . "zhadum.ezt" );
  170. $src = new ezcTemplateSourceCode( $this->templateStorePath . "zhadum.ezt",
  171. "planet:zhadum.ezt", false );
  172. self::assertTrue( file_exists( $this->templateStorePath . "zhadum.ezt" ), 'Template file does not exist on filesystem' );
  173. self::assertTrue( is_writeable( $this->templateStorePath . "zhadum.ezt" ), 'Template file cannot be written to' );
  174. self::assertSame( true, $src->isWriteable(), 'Template file is not available while the file exists' );
  175. }
  176. /**
  177. * Test isWriteable() with unwriteable file
  178. */
  179. public function testIsNotWriteable()
  180. {
  181. // If running as root you can always write, so this test should be
  182. // skipped when running as root.
  183. if ( !ezcBaseFeatures::hasFunction("posix_getuid") || posix_getuid() == 0 )
  184. {
  185. return;
  186. }
  187. copy( $this->templatePath . "zhadum.ezt",
  188. $this->templateStorePath . "zhadum.ezt" );
  189. // This only works on Linux/Unix, what to do here on other platforms?
  190. $old = umask( 0 );
  191. chmod( $this->templateStorePath . "zhadum.ezt", 0444 );
  192. umask( $old );
  193. $src = new ezcTemplateSourceCode( $this->templateStorePath . "zhadum.ezt",
  194. "planet:zhadum.ezt", false );
  195. self::assertTrue( file_exists( $this->templateStorePath . "zhadum.ezt" ), 'Template file does not exist on filesystem' );
  196. self::assertTrue( !is_writeable( $this->templateStorePath . "zhadum.ezt" ), 'Template file is still writeable' );
  197. self::assertSame( false, $src->isWriteable(), 'Unwriteable template file is considered writeable' );
  198. }
  199. /**
  200. * Test loading code from template file
  201. */
  202. public function testLoad()
  203. {
  204. $src = new ezcTemplateSourceCode( $this->templatePath . "zhadum.ezt",
  205. "planet:zhadum.ezt", "definitely not the source from the file zhadum.ezt" );
  206. $src->load();
  207. self::assertSame( $this->templatePath . "zhadum.ezt", $src->stream, 'Property <stream> does not return correct value.' );
  208. self::assertSame( "planet:zhadum.ezt", $src->resource, 'Property <resource> does not return correct value.' );
  209. self::assertSame( "A planet far far away.\n{\$planet.name}\n", $src->code, 'Property <code> does not return correct value.' );
  210. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  211. }
  212. /**
  213. * Test loading code from template file which does not exist.
  214. * In this case an exception is expected to be thrown.
  215. */
  216. public function testLoadNonExistant()
  217. {
  218. $src = new ezcTemplateSourceCode( $this->templatePath . "zhadum_no_such_file.tpl",
  219. "planet:zhadum.ezt", "definitely not the source from the file zhadum.ezt" );
  220. try
  221. {
  222. $src->load();
  223. self::fail( "No exception thrown for non existant file" );
  224. }
  225. catch ( ezcTemplateFileNotFoundException $e )
  226. {
  227. }
  228. self::assertSame( $this->templatePath . "zhadum_no_such_file.tpl", $src->stream, 'Property <stream> does not return correct value.' );
  229. self::assertSame( "planet:zhadum.ezt", $src->resource, 'Property <resource> does not return correct value.' );
  230. self::assertSame( "definitely not the source from the file zhadum.ezt", $src->code, 'Property <code> does not return correct value.' );
  231. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  232. }
  233. /**
  234. * Test loading code from template file which does not exist.
  235. * In this case an exception is expected to be thrown.
  236. */
  237. public function testLoadNonReadable()
  238. {
  239. // If running as root you can always write, so this test should be
  240. // skipped when running as root.
  241. if ( !ezcBaseFeatures::hasFunction("posix_getuid") || posix_getuid() == 0 )
  242. {
  243. return;
  244. }
  245. copy( $this->templatePath . "zhadum.ezt",
  246. $this->templateStorePath . "zhadum.ezt" );
  247. // This only works on Linux/Unix, what to do here on other platforms?
  248. $old = umask( 0 );
  249. chmod( $this->templateStorePath . "zhadum.ezt", 0222 );
  250. umask( $old );
  251. $src = new ezcTemplateSourceCode( $this->templateStorePath . "zhadum.ezt",
  252. "planet:zhadum.ezt", "definitely not the source from the file zhadum.ezt" );
  253. try
  254. {
  255. $src->load();
  256. self::fail( "No exception thrown for non-readable file" );
  257. }
  258. catch ( ezcTemplateFileNotReadableException $e )
  259. {
  260. }
  261. self::assertSame( $this->templateStorePath . "zhadum.ezt", $src->stream, 'Property <stream> does not return correct value.' );
  262. self::assertSame( "planet:zhadum.ezt", $src->resource, 'Property <resource> does not return correct value.' );
  263. self::assertSame( "definitely not the source from the file zhadum.ezt", $src->code, 'Property <code> does not return correct value.' );
  264. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  265. }
  266. /**
  267. * Test saving code to template file
  268. */
  269. public function testSave()
  270. {
  271. $src = new ezcTemplateSourceCode( $this->templateStorePath . "zhadum.ezt",
  272. "planet:zhadum.ezt", "I would not go there if I were you.\nJust a friendly advice.\n" );
  273. self::assertTrue( !file_exists( $this->templateStorePath . "zhadum.ezt" ),
  274. 'Stored template file <' . $this->templateStorePath . 'zhadum.ezt> already exists, cannot run test.' );
  275. $src->save();
  276. self::assertSame( $this->templateStorePath . "zhadum.ezt", $src->stream,
  277. 'Property <stream> does not return correct value.' );
  278. self::assertSame( "planet:zhadum.ezt", $src->resource,
  279. 'Property <resource> does not return correct value.' );
  280. self::assertSame( "I would not go there if I were you.\nJust a friendly advice.\n", $src->code,
  281. 'Property <code> does not return correct value.' );
  282. self::assertSame( false, file_exists( $this->templateStorePath . "#zhadum.ezt#" ),
  283. 'Temporary template file <' . $this->templateStorePath . '#zhadum.ezt#> still exist after save()' );
  284. self::assertSame( true, file_exists( $this->templateStorePath . "zhadum.ezt" ),
  285. 'Stored template file <' . $this->templateStorePath . 'zhadum.ezt> does not exist after save()' );
  286. self::assertSame( $src->code, file_get_contents( $this->templateStorePath . "zhadum.ezt" ),
  287. 'File <' . $this->templateStorePath . 'zhadum.ezt> does not contain the correct source code.' );
  288. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  289. // Now try to overwrite the same file, a backup should be made
  290. $src2 = new ezcTemplateSourceCode( $this->templateStorePath . "zhadum.ezt",
  291. "planet:zhadum.ezt", "Some new content for the template." );
  292. $src2->save();
  293. self::assertSame( $this->templateStorePath . "zhadum.ezt", $src2->stream,
  294. 'Property <stream> does not return correct value.' );
  295. self::assertSame( "planet:zhadum.ezt", $src2->resource,
  296. 'Property <resource> does not return correct value.' );
  297. self::assertSame( "Some new content for the template.", $src2->code,
  298. 'Property <code> does not return correct value.' );
  299. // check contents
  300. self::assertSame( false, file_exists( $this->templateStorePath . "#zhadum.ezt#" ),
  301. 'Temporary template file <' . $this->templateStorePath . '#zhadum.ezt#> still exist after save()' );
  302. self::assertSame( true, file_exists( $this->templateStorePath . "zhadum.ezt" ),
  303. 'Stored template file <' . $this->templateStorePath . 'zhadum.ezt> does not exist after save()' );
  304. self::assertSame( $src2->code, file_get_contents( $this->templateStorePath . "zhadum.ezt" ),
  305. 'File <' . $this->templateStorePath . 'zhadum.ezt> does not contain the correct source code.' );
  306. // check contents of backup
  307. self::assertSame( true, file_exists( $this->templateStorePath . "zhadum.ezt~" ),
  308. 'Stored template file <' . $this->templateStorePath . 'zhadum.ezt> does not exist after save()' );
  309. self::assertSame( $src->code, file_get_contents( $this->templateStorePath . "zhadum.ezt~" ),
  310. 'File <' . $this->templateStorePath . 'zhadum.ezt> does not contain the correct source code.' );
  311. // Now try to overwrite the same file again, a backup should be made and the old backup removed
  312. $src3 = new ezcTemplateSourceCode( $this->templateStorePath . "zhadum.ezt",
  313. "planet:zhadum.ezt", "Some updated content for the template." );
  314. $src3->save();
  315. self::assertSame( $this->templateStorePath . "zhadum.ezt", $src3->stream,
  316. 'Property <stream> does not return correct value.' );
  317. self::assertSame( "planet:zhadum.ezt", $src3->resource,
  318. 'Property <resource> does not return correct value.' );
  319. self::assertSame( "Some updated content for the template.", $src3->code,
  320. 'Property <code> does not return correct value.' );
  321. // check contents
  322. self::assertSame( false, file_exists( $this->templateStorePath . "#zhadum.ezt#" ),
  323. 'Temporary template file <' . $this->templateStorePath . '#zhadum.ezt#> still exist after save()' );
  324. self::assertSame( true, file_exists( $this->templateStorePath . "zhadum.ezt" ),
  325. 'Stored template file <' . $this->templateStorePath . 'zhadum.ezt> does not exist after save()' );
  326. self::assertSame( $src3->code, file_get_contents( $this->templateStorePath . "zhadum.ezt" ),
  327. 'File <' . $this->templateStorePath . 'zhadum.ezt> does not contain the correct source code.' );
  328. // check contents of backup
  329. self::assertSame( true, file_exists( $this->templateStorePath . "zhadum.ezt~" ),
  330. 'Stored template file <' . $this->templateStorePath . 'zhadum.ezt> does not exist after save()' );
  331. self::assertSame( $src2->code, file_get_contents( $this->templateStorePath . "zhadum.ezt~" ),
  332. 'File <' . $this->templateStorePath . 'zhadum.ezt> does not contain the correct source code.' );
  333. // self::assertSame( null, $src2->context, 'Property <context> does not return correct value.' );
  334. }
  335. /**
  336. * Test saving code to template file
  337. */
  338. public function testSaveNonWriteable()
  339. {
  340. // If running as root you can always write, so this test should be
  341. // skipped when running as root.
  342. if ( !ezcBaseFeatures::hasFunction("posix_getuid") || posix_getuid() == 0 )
  343. {
  344. return;
  345. }
  346. copy( $this->templatePath . "zhadum.ezt",
  347. $this->templateStorePath . "zhadum.ezt" );
  348. // This only works on Linux/Unix, what to do here on other platforms?
  349. $old = umask( 0 );
  350. chmod( $this->templateStorePath . "zhadum.ezt", 0444 );
  351. umask( $old );
  352. $src = new ezcTemplateSourceCode( $this->templateStorePath . "zhadum.ezt",
  353. "planet:zhadum.ezt", "I would not go there if I were you.\nJust a friendly advice.\n" );
  354. self::assertTrue( file_exists( $this->templateStorePath . "zhadum.ezt" ), 'Stored template file <' . $this->templateStorePath . 'zhadum.ezt> does not exist, cannot run save test.' );
  355. self::assertTrue( !is_writeable( $this->templateStorePath . "zhadum.ezt" ), 'Stored template file <' . $this->templateStorePath . 'zhadum.ezt> should not be writable, cannot run save test.' );
  356. try
  357. {
  358. $src->save();
  359. self::fail( "No exception thrown for non-writeable file" );
  360. }
  361. catch ( ezcTemplateFileNotWriteableException $e )
  362. {
  363. }
  364. self::assertSame( $this->templateStorePath . "zhadum.ezt", $src->stream, 'Property <stream> does not return correct value.' );
  365. self::assertSame( "planet:zhadum.ezt", $src->resource, 'Property <resource> does not return correct value.' );
  366. self::assertSame( "I would not go there if I were you.\nJust a friendly advice.\n", $src->code, 'Property <code> does not return correct value.' );
  367. self::assertSame( "A planet far far away.\n{\$planet.name}\n", file_get_contents( $src->stream ), 'Original file does no longer contain the correct value.' );
  368. self::assertSame( true, file_exists( $this->templateStorePath . "zhadum.ezt" ), 'Stored template file <' . $this->templateStorePath . 'zhadum.ezt> does not exist after save()' );
  369. self::assertSame( "A planet far far away.\n{\$planet.name}\n", file_get_contents( $this->templateStorePath . "zhadum.ezt" ), 'File <' . $this->templateStorePath . 'zhadum.ezt> does not contain the original source code, file was probably written to (which should not happen).' );
  370. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  371. }
  372. /**
  373. * Test deleting a temporary template file
  374. */
  375. public function testDelete()
  376. {
  377. copy( $this->templatePath . "zhadum.ezt",
  378. $this->templateStorePath . "zhadum.ezt" );
  379. $src = new ezcTemplateSourceCode( $this->templateStorePath . "zhadum.ezt",
  380. "planet:zhadum.ezt", "The abyss." );
  381. self::assertTrue( file_exists( $this->templateStorePath . "zhadum.ezt" ), 'Temporary template file <' . $this->templateStorePath . 'zhadum.ezt> does not exists, copy failed.' );
  382. $src->delete();
  383. self::assertSame( $this->templateStorePath . "zhadum.ezt", $src->stream, 'Property <stream> does not return correct value.' );
  384. self::assertSame( "planet:zhadum.ezt", $src->resource, 'Property <resource> does not return correct value.' );
  385. self::assertSame( "The abyss.", $src->code, 'Property <code> does not return correct value.' );
  386. self::assertSame( false, file_exists( $this->templateStorePath . "zhadum.ezt" ), 'Temporary template file <' . $this->templateStorePath . 'zhadum.ezt> still exist, was supposed to be unlinked.' );
  387. self::assertSame( false, $src->isAvailable(), 'Temporary template file <' . $this->templateStorePath . 'zhadum.ezt> does not return false in isAvailable().' );
  388. // self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
  389. }
  390. }
  391. ?>