You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wizard.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Arthur Schiwon
  6. * @copyright 2014 Arthur Schiwon blizzz@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\user_ldap\tests;
  23. use \OCA\user_ldap\lib\Wizard;
  24. // use \OCA\user_ldap\USER_LDAP as UserLDAP;
  25. // use \OCA\user_ldap\lib\Access;
  26. // use \OCA\user_ldap\lib\Configuration;
  27. // use \OCA\user_ldap\lib\ILDAPWrapper;
  28. class Test_Wizard extends \PHPUnit_Framework_TestCase {
  29. public function setUp() {
  30. //we need to make sure the consts are defined, otherwise tests will fail
  31. //on systems without php5_ldap
  32. $ldapConsts = array('LDAP_OPT_PROTOCOL_VERSION',
  33. 'LDAP_OPT_REFERRALS', 'LDAP_OPT_NETWORK_TIMEOUT');
  34. foreach($ldapConsts as $const) {
  35. if(!defined($const)) {
  36. define($const, 42);
  37. }
  38. }
  39. }
  40. private function getWizardAndMocks() {
  41. static $conMethods;
  42. if(is_null($conMethods)) {
  43. $conMethods = get_class_methods('\OCA\user_ldap\lib\Configuration');
  44. }
  45. $lw = $this->getMock('\OCA\user_ldap\lib\ILDAPWrapper');
  46. $conf = $this->getMock('\OCA\user_ldap\lib\Configuration',
  47. $conMethods,
  48. array($lw, null, null));
  49. return array(new Wizard($conf, $lw), $conf, $lw);
  50. }
  51. private function prepareLdapWrapperForConnections(&$ldap) {
  52. $ldap->expects($this->once())
  53. ->method('connect')
  54. //dummy value, usually invalid
  55. ->will($this->returnValue(true));
  56. $ldap->expects($this->exactly(3))
  57. ->method('setOption')
  58. ->will($this->returnValue(true));
  59. $ldap->expects($this->once())
  60. ->method('bind')
  61. ->will($this->returnValue(true));
  62. }
  63. public function testCumulativeSearchOnAttributeLimited() {
  64. list($wizard, $configuration, $ldap) = $this->getWizardAndMocks();
  65. $configuration->expects($this->any())
  66. ->method('__get')
  67. ->will($this->returnCallback(function($name) {
  68. if($name === 'ldapBase') {
  69. return array('base');
  70. }
  71. return null;
  72. }));
  73. $this->prepareLdapWrapperForConnections($ldap);
  74. $ldap->expects($this->any())
  75. ->method('isResource')
  76. ->will($this->returnValue(true));
  77. $ldap->expects($this->exactly(2))
  78. ->method('search')
  79. //dummy value, usually invalid
  80. ->will($this->returnValue(true));
  81. $ldap->expects($this->exactly(2))
  82. ->method('countEntries')
  83. //an is_resource check will follow, so we need to return a dummy resource
  84. ->will($this->returnValue(23));
  85. //5 DNs per filter means 2x firstEntry and 8x nextEntry
  86. $ldap->expects($this->exactly(2))
  87. ->method('firstEntry')
  88. //dummy value, usually invalid
  89. ->will($this->returnValue(true));
  90. $ldap->expects($this->exactly(8))
  91. ->method('nextEntry')
  92. //dummy value, usually invalid
  93. ->will($this->returnValue(true));
  94. $ldap->expects($this->exactly(10))
  95. ->method('getAttributes')
  96. //dummy value, usually invalid
  97. ->will($this->returnValue(array('cn' => array('foo'), 'count' => 1)));
  98. global $uidnumber;
  99. $uidnumber = 1;
  100. $ldap->expects($this->exactly(10))
  101. ->method('getDN')
  102. //dummy value, usually invalid
  103. ->will($this->returnCallback(function($a, $b) {
  104. global $uidnumber;
  105. return $uidnumber++;
  106. }));
  107. # The following expectations are the real test #
  108. $filters = array('f1', 'f2', '*');
  109. $wizard->cumulativeSearchOnAttribute($filters, 'cn', true, 5);
  110. unset($uidnumber);
  111. }
  112. public function testCumulativeSearchOnAttributeUnlimited() {
  113. list($wizard, $configuration, $ldap) = $this->getWizardAndMocks();
  114. $configuration->expects($this->any())
  115. ->method('__get')
  116. ->will($this->returnCallback(function($name) {
  117. if($name === 'ldapBase') {
  118. return array('base');
  119. }
  120. return null;
  121. }));
  122. $this->prepareLdapWrapperForConnections($ldap);
  123. $ldap->expects($this->any())
  124. ->method('isResource')
  125. ->will($this->returnCallback(function($r) {
  126. if($r === true) {
  127. return true;
  128. }
  129. if($r % 24 === 0) {
  130. global $uidnumber;
  131. $uidnumber++;
  132. return false;
  133. }
  134. return true;
  135. }));
  136. $ldap->expects($this->exactly(2))
  137. ->method('search')
  138. //dummy value, usually invalid
  139. ->will($this->returnValue(true));
  140. $ldap->expects($this->exactly(2))
  141. ->method('countEntries')
  142. //an is_resource check will follow, so we need to return a dummy resource
  143. ->will($this->returnValue(23));
  144. //5 DNs per filter means 2x firstEntry and 8x nextEntry
  145. $ldap->expects($this->exactly(2))
  146. ->method('firstEntry')
  147. //dummy value, usually invalid
  148. ->will($this->returnCallback(function($r) {
  149. global $uidnumber;
  150. return $uidnumber;
  151. }));
  152. $ldap->expects($this->exactly(46))
  153. ->method('nextEntry')
  154. //dummy value, usually invalid
  155. ->will($this->returnCallback(function($r) {
  156. global $uidnumber;
  157. return $uidnumber;
  158. }));
  159. $ldap->expects($this->exactly(46))
  160. ->method('getAttributes')
  161. //dummy value, usually invalid
  162. ->will($this->returnValue(array('cn' => array('foo'), 'count' => 1)));
  163. global $uidnumber;
  164. $uidnumber = 1;
  165. $ldap->expects($this->exactly(46))
  166. ->method('getDN')
  167. //dummy value, usually invalid
  168. ->will($this->returnCallback(function($a, $b) {
  169. global $uidnumber;
  170. return $uidnumber++;
  171. }));
  172. # The following expectations are the real test #
  173. $filters = array('f1', 'f2', '*');
  174. $wizard->cumulativeSearchOnAttribute($filters, 'cn', true, 0);
  175. unset($uidnumber);
  176. }
  177. }