metadata.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Arthur Schiwon <blizzz@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Group;
  9. class Test_MetaData extends \Test\TestCase {
  10. private function getGroupManagerMock() {
  11. return $this->getMockBuilder('\OC\Group\Manager')
  12. ->disableOriginalConstructor()
  13. ->getMock();
  14. }
  15. private function getGroupMock() {
  16. $group = $this->getMockBuilder('\OC\Group\Group')
  17. ->disableOriginalConstructor()
  18. ->getMock();
  19. $group->expects($this->exactly(9))
  20. ->method('getGID')
  21. ->will($this->onConsecutiveCalls(
  22. 'admin', 'admin', 'admin',
  23. 'g2', 'g2', 'g2',
  24. 'g3', 'g3', 'g3'));
  25. $group->expects($this->exactly(3))
  26. ->method('count')
  27. ->with('')
  28. ->will($this->onConsecutiveCalls(2, 3, 5));
  29. return $group;
  30. }
  31. public function testGet() {
  32. $groupManager = $this->getGroupManagerMock();
  33. $groupMetaData = new \OC\Group\MetaData('foo', true, $groupManager);
  34. $group = $this->getGroupMock();
  35. $groups = array_fill(0, 3, $group);
  36. $groupManager->expects($this->once())
  37. ->method('search')
  38. ->with('')
  39. ->will($this->returnValue($groups));
  40. list($adminGroups, $ordinaryGroups) = $groupMetaData->get();
  41. $this->assertSame(1, count($adminGroups));
  42. $this->assertSame(2, count($ordinaryGroups));
  43. $this->assertSame('g2', $ordinaryGroups[0]['name']);
  44. $this->assertSame(3, $ordinaryGroups[0]['usercount']);
  45. }
  46. public function testGetWithSorting() {
  47. $groupManager = $this->getGroupManagerMock();
  48. $groupMetaData = new \OC\Group\MetaData('foo', true, $groupManager);
  49. $groupMetaData->setSorting($groupMetaData::SORT_USERCOUNT);
  50. $group = $this->getGroupMock();
  51. $groups = array_fill(0, 3, $group);
  52. $groupManager->expects($this->once())
  53. ->method('search')
  54. ->with('')
  55. ->will($this->returnValue($groups));
  56. list($adminGroups, $ordinaryGroups) = $groupMetaData->get();
  57. $this->assertSame(1, count($adminGroups));
  58. $this->assertSame(2, count($ordinaryGroups));
  59. $this->assertSame('g3', $ordinaryGroups[0]['name']);
  60. $this->assertSame(5, $ordinaryGroups[0]['usercount']);
  61. }
  62. public function testGetWithCache() {
  63. $groupManager = $this->getGroupManagerMock();
  64. $groupMetaData = new \OC\Group\MetaData('foo', true, $groupManager);
  65. $group = $this->getGroupMock();
  66. $groups = array_fill(0, 3, $group);
  67. $groupManager->expects($this->once())
  68. ->method('search')
  69. ->with('')
  70. ->will($this->returnValue($groups));
  71. //two calls, if caching fails call counts for group and groupmanager
  72. //are exceeded
  73. $groupMetaData->get();
  74. $groupMetaData->get();
  75. }
  76. //get() does not need to be tested with search parameters, because they are
  77. //solely and only passed to GroupManager and Group.
  78. }