/Authentication/tests/filters/group/group_multiple_test.php

https://github.com/F5/zetacomponents · PHP · 162 lines · 100 code · 30 blank · 32 comment · 2 complexity · 705eef6d8c225ecfd953a16ca0d6bd75 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. include_once( 'Authentication/tests/test.php' );
  28. /**
  29. * @package Authentication
  30. * @version //autogen//
  31. * @subpackage Tests
  32. */
  33. class ezcAuthenticationGroupMultipleTest extends ezcAuthenticationTest
  34. {
  35. protected static $data1;
  36. protected static $data2;
  37. protected static $results;
  38. public static function suite()
  39. {
  40. self::$data1 = array( // id_credentials, encrypted_token, token_method
  41. array( 'qwerty', 'b1b3773a05c0ed0176787a4f1574ff0075f7521e', 'sha1' ),
  42. array( 'wrong value', 'b1b3773a05c0ed0176787a4f1574ff0075f7521e', 'sha1' ),
  43. );
  44. self::$data2 = array( // id_credentials, encrypted_token, token_method
  45. array( 'asdfgh', 'a152e841783914146e4bcd4f39100686', 'md5' ),
  46. array( 'wrong value', 'a152e841783914146e4bcd4f39100686', 'md5' ),
  47. );
  48. self::$results = array( // the first 2 values are keys in $data1 and $data2
  49. // the 3rd value is the mode for the Group filter
  50. // the 4th value is the expected result for assertEquals()
  51. array( 0, 0, ezcAuthenticationGroupFilter::MODE_AND, true ),
  52. array( 0, 1, ezcAuthenticationGroupFilter::MODE_AND, false ),
  53. array( 0, 0, ezcAuthenticationGroupFilter::MODE_OR, true ),
  54. array( 0, 1, ezcAuthenticationGroupFilter::MODE_OR, true ),
  55. array( 1, 0, ezcAuthenticationGroupFilter::MODE_AND, false ),
  56. array( 1, 1, ezcAuthenticationGroupFilter::MODE_AND, false ),
  57. array( 1, 0, ezcAuthenticationGroupFilter::MODE_OR, true ),
  58. array( 1, 1, ezcAuthenticationGroupFilter::MODE_OR, false ),
  59. );
  60. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  61. }
  62. public function testGroupMultipleCredentialsAddFilter()
  63. {
  64. foreach ( self::$results as $result )
  65. {
  66. $credentials1 = new ezcAuthenticationIdCredentials( self::$data1[$result[0]][0] );
  67. $credentials2 = new ezcAuthenticationIdCredentials( self::$data2[$result[1]][0] );
  68. $authentication = new ezcAuthentication( $credentials1 );
  69. $filter1 = new ezcAuthenticationTokenFilter( self::$data1[$result[0]][1], self::$data1[$result[0]][2] );
  70. $filter2 = new ezcAuthenticationTokenFilter( self::$data2[$result[1]][1], self::$data2[$result[1]][2] );
  71. $options = new ezcAuthenticationGroupOptions();
  72. $options->multipleCredentials = true;
  73. $options->mode = $result[2];
  74. $group = new ezcAuthenticationGroupFilter( array(), $options );
  75. $group->addFilter( $filter1, $credentials1 );
  76. $group->addFilter( $filter2, $credentials2 );
  77. $authentication->addFilter( $group );
  78. $this->assertEquals( $result[3], $authentication->run(), "Test failed for ({$result[0]}, {$result[1]}, {$result[2]})." );
  79. }
  80. }
  81. public function testGroupMultipleCredentialsConstructor()
  82. {
  83. foreach ( self::$results as $result )
  84. {
  85. $credentials1 = new ezcAuthenticationIdCredentials( self::$data1[$result[0]][0] );
  86. $credentials2 = new ezcAuthenticationIdCredentials( self::$data2[$result[1]][0] );
  87. $authentication = new ezcAuthentication( $credentials1 );
  88. $filter1 = new ezcAuthenticationTokenFilter( self::$data1[$result[0]][1], self::$data1[$result[0]][2] );
  89. $filter2 = new ezcAuthenticationTokenFilter( self::$data2[$result[1]][1], self::$data2[$result[1]][2] );
  90. $options = new ezcAuthenticationGroupOptions();
  91. $options->multipleCredentials = true;
  92. $options->mode = $result[2];
  93. $group = new ezcAuthenticationGroupFilter( array( array( $filter1, $credentials1 ), array( $filter2, $credentials2 ) ), $options );
  94. $authentication->addFilter( $group );
  95. $this->assertEquals( $result[3], $authentication->run(), "Test failed for ({$result[0]}, {$result[1]}, {$result[2]})." );
  96. }
  97. }
  98. public function testGroupMultipleCredentialsFailAddFilterMissingCredentials()
  99. {
  100. $filter1 = new ezcAuthenticationTokenFilter( self::$data1[0][1], self::$data1[0][2] );
  101. $filter2 = new ezcAuthenticationTokenFilter( self::$data2[1][1], self::$data2[1][2] );
  102. $options = new ezcAuthenticationGroupOptions();
  103. $options->multipleCredentials = true;
  104. $group = new ezcAuthenticationGroupFilter( array(), $options );
  105. try
  106. {
  107. $group->addFilter( $filter1 );
  108. $this->fail( 'Expected exception was not thrown.' );
  109. }
  110. catch ( ezcAuthenticationException $e )
  111. {
  112. $this->assertSame( 'A credentials object must be specified for each filter when the multipleCredentials option is enabled.', $e->getMessage() );
  113. }
  114. }
  115. public function testGroupMultipleCredentialsFailConstructorMissingCredentials()
  116. {
  117. $filter1 = new ezcAuthenticationTokenFilter( self::$data1[0][1], self::$data1[0][2] );
  118. $filter2 = new ezcAuthenticationTokenFilter( self::$data2[1][1], self::$data2[1][2] );
  119. $options = new ezcAuthenticationGroupOptions();
  120. $options->multipleCredentials = true;
  121. try
  122. {
  123. $group = new ezcAuthenticationGroupFilter( array( $filter1, $filter2 ), $options );
  124. $this->fail( 'Expected exception was not thrown.' );
  125. }
  126. catch ( ezcAuthenticationException $e )
  127. {
  128. $this->assertEquals( 'A credentials object must be specified for each filter when the multipleCredentials option is enabled.', $e->getMessage() );
  129. }
  130. }
  131. }
  132. ?>