/Authentication/tests/test.php

https://github.com/F5/zetacomponents · PHP · 297 lines · 150 code · 18 blank · 129 comment · 5 complexity · 730c28545adbfc4827d1f559f4157cb2 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. * @filesource
  23. * @package Authentication
  24. * @version //autogen//
  25. * @subpackage Tests
  26. */
  27. /**
  28. * @package Authentication
  29. * @version //autogen//
  30. * @subpackage Tests
  31. * @access private
  32. */
  33. class ezcAuthenticationTest extends ezcTestCase
  34. {
  35. /**
  36. * Tests assigning an invalid value to a property.
  37. *
  38. * Expects that an ezcBaseValueException is raised by the invalid value.
  39. *
  40. * @param object $properties An object which implements properties access
  41. * @param string $property The property of the $properties object to test
  42. * @param mixed $value The value to try to assign to $property
  43. * @param string $allowedValue The values which are allowed for $property
  44. */
  45. protected function invalidPropertyTest( $properties, $property, $value, $allowedValue )
  46. {
  47. try
  48. {
  49. $properties->$property = $value;
  50. $this->fail( "Expected exception was not thrown." );
  51. }
  52. catch ( ezcBaseValueException $e )
  53. {
  54. $value = is_array( $value ) ? serialize( $value ) : $value;
  55. $this->assertEquals( "The value '{$value}' that you were trying to assign to setting '{$property}' is invalid. Allowed values are: {$allowedValue}.", $e->getMessage() );
  56. }
  57. }
  58. /**
  59. * Tests assigning a value to a missing property.
  60. *
  61. * Expects that an ezcBasePropertyNotFoundException is raised by the missing
  62. * property.
  63. *
  64. * @param object $properties An object which implements properties access
  65. * @param string $property The property of the $properties object to test
  66. */
  67. protected function missingPropertyTest( $properties, $property )
  68. {
  69. try
  70. {
  71. $properties->$property = null;
  72. $this->fail( "Expected exception was not thrown." );
  73. }
  74. catch ( ezcBasePropertyNotFoundException $e )
  75. {
  76. $this->assertEquals( "No such property name '{$property}'.", $e->getMessage() );
  77. }
  78. // workaround around a bug (?) - __isset() in ezcBaseOptions complains and warns
  79. // that the second parameter for array_exists() must be an array or an object
  80. if ( !$properties instanceof ezcBaseOptions )
  81. {
  82. try
  83. {
  84. $value = $properties->$property;
  85. $this->fail( "Expected exception was not thrown." );
  86. }
  87. catch ( ezcBasePropertyNotFoundException $e )
  88. {
  89. $this->assertEquals( "No such property name '{$property}'.", $e->getMessage() );
  90. }
  91. }
  92. }
  93. /**
  94. * Tests if a property is set.
  95. *
  96. * Compares the result of isset() with $value.
  97. *
  98. * @param object $properties An object which implements properties access
  99. * @param string $property The property of the $properties object to test
  100. * @param bool $value True if expecting that $property is set, false otherwise
  101. */
  102. protected function issetPropertyTest( $properties, $property, $value )
  103. {
  104. $this->assertEquals( $value, isset( $properties->$property ) );
  105. }
  106. /**
  107. * Tests assigning a non-existent path to a property.
  108. *
  109. * Expects that an ezcBaseFileNotFoundException is raised by the missing
  110. * path.
  111. *
  112. * @param object $properties An object which implements properties access
  113. * @param string $property The property of the $properties object to test
  114. * @param string $value A path which does not exist
  115. */
  116. protected function missingFileTest( $properties, $property, $value )
  117. {
  118. try
  119. {
  120. $properties->$property = $value;
  121. $this->fail( "Expected exception was not thrown" );
  122. }
  123. catch ( ezcBaseFileNotFoundException $e )
  124. {
  125. $this->assertEquals( "The file '{$value}' could not be found.", $e->getMessage() );
  126. }
  127. }
  128. /**
  129. * Tests assigning an unreadable path to a property.
  130. *
  131. * Expects that an ezcBaseFilePermissionException is raised by the unreadable
  132. * path.
  133. *
  134. * This function creates a temporary file and makes it unreadable.
  135. *
  136. * @param object $properties An object which implements properties access
  137. * @param string $property The property of the $properties object to test
  138. * @param string $value A filename without paths or slashes
  139. */
  140. protected function unreadableFileTest( $properties, $property, $value )
  141. {
  142. $tempDir = $this->createTempDir( get_class( $this ) );
  143. $path = $tempDir . DIRECTORY_SEPARATOR . $value;
  144. $fh = fopen( $path, "wb" );
  145. fwrite( $fh, "some values" );
  146. fclose( $fh );
  147. chmod( $path, 0 );
  148. try
  149. {
  150. $properties->$property = $path;
  151. $this->removeTempDir();
  152. $this->fail( "Expected exception was not thrown." );
  153. }
  154. catch ( ezcBaseFilePermissionException $e )
  155. {
  156. $this->assertEquals( "The file '{$path}' can not be opened for reading.", $e->getMessage() );
  157. }
  158. $this->removeTempDir();
  159. }
  160. /**
  161. * Tests assigning an unwritable path to a property.
  162. *
  163. * Expects that an ezcBaseFilePermissionException is raised by the unwritable
  164. * path.
  165. *
  166. * This function creates a temporary file and makes it unwritable.
  167. *
  168. * @param object $properties An object which implements properties access
  169. * @param string $property The property of the $properties object to test
  170. * @param string $value A filename without paths or slashes
  171. */
  172. protected function unwritableFileTest( $properties, $property, $value )
  173. {
  174. $tempDir = $this->createTempDir( get_class( $this ) );
  175. $path = $tempDir . DIRECTORY_SEPARATOR . $value;
  176. $fh = fopen( $path, "wb" );
  177. fwrite( $fh, "some values" );
  178. fclose( $fh );
  179. chmod( $path, 0 );
  180. try
  181. {
  182. $properties->$property = $path;
  183. $this->removeTempDir();
  184. $this->fail( "Expected exception was not thrown." );
  185. }
  186. catch ( ezcBaseFilePermissionException $e )
  187. {
  188. $this->assertEquals( "The file '{$path}' can not be opened for writing.", $e->getMessage() );
  189. }
  190. $this->removeTempDir();
  191. }
  192. /**
  193. * Tests assigning a non-existent directory to a property.
  194. *
  195. * Expects that an ezcBaseFileNotFoundException is raised by the missing
  196. * directory.
  197. *
  198. * @param object $properties An object which implements properties access
  199. * @param string $property The property of the $properties object to test
  200. * @param string $value A directory which does not exist
  201. */
  202. protected function missingDirTest( $properties, $property, $value )
  203. {
  204. try
  205. {
  206. $properties->$property = $value;
  207. $this->fail( "Expected exception was not thrown" );
  208. }
  209. catch ( ezcBaseFileNotFoundException $e )
  210. {
  211. $this->assertEquals( "The file '{$value}' could not be found.", $e->getMessage() );
  212. }
  213. }
  214. /**
  215. * Tests assigning an unreadable directory to a property.
  216. *
  217. * Expects that an ezcBaseFilePermissionException is raised by the unreadable
  218. * path.
  219. *
  220. * This function creates a temporary directory and makes it unreadable.
  221. *
  222. * @param object $properties An object which implements properties access
  223. * @param string $property The property of the $properties object to test
  224. * @param string $value A directory name without paths or slashes
  225. */
  226. protected function unreadableDirTest( $properties, $property, $value )
  227. {
  228. $tempDir = $this->createTempDir( get_class( $this ) );
  229. $path = $tempDir . DIRECTORY_SEPARATOR . $value;
  230. mkdir( $path );
  231. chmod( $path, 0 );
  232. try
  233. {
  234. $properties->$property = $path;
  235. chmod( $path, 0777 );
  236. $this->removeTempDir();
  237. $this->fail( "Expected exception was not thrown." );
  238. }
  239. catch ( ezcBaseFilePermissionException $e )
  240. {
  241. $this->assertEquals( "The file '{$path}' can not be opened for reading.", $e->getMessage() );
  242. }
  243. chmod( $path, 0777 );
  244. $this->removeTempDir();
  245. }
  246. /**
  247. * Tests assigning an unwritable directory to a property.
  248. *
  249. * Expects that an ezcBaseFilePermissionException is raised by the unwritable
  250. * path.
  251. *
  252. * This function creates a temporary directory and makes it unwritable.
  253. *
  254. * @param object $properties An object which implements properties access
  255. * @param string $property The property of the $properties object to test
  256. * @param string $value A directory name without paths or slashes
  257. */
  258. protected function unwritableDirTest( $properties, $property, $value )
  259. {
  260. $tempDir = $this->createTempDir( get_class( $this ) );
  261. $path = $tempDir . DIRECTORY_SEPARATOR . $value;
  262. mkdir( $path );
  263. chmod( $path, 0444 );
  264. try
  265. {
  266. $properties->$property = $path;
  267. chmod( $path, 0777 );
  268. $this->removeTempDir();
  269. $this->fail( "Expected exception was not thrown." );
  270. }
  271. catch ( ezcBaseFilePermissionException $e )
  272. {
  273. $this->assertEquals( "The file '{$path}' can not be opened for writing.", $e->getMessage() );
  274. }
  275. chmod( $path, 0777 );
  276. $this->removeTempDir();
  277. }
  278. }
  279. ?>