1
0

ManagerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Tests\Contacts\ContactsMenu;
  24. use OC\Contacts\ContactsMenu\ActionProviderStore;
  25. use OC\Contacts\ContactsMenu\ContactsStore;
  26. use OC\Contacts\ContactsMenu\Manager;
  27. use OCP\App\IAppManager;
  28. use OCP\Contacts\ContactsMenu\IEntry;
  29. use OCP\Contacts\ContactsMenu\IProvider;
  30. use OCP\IUser;
  31. use PHPUnit_Framework_MockObject_MockObject;
  32. use Test\TestCase;
  33. class ManagerTest extends TestCase {
  34. /** @var ContactsStore|PHPUnit_Framework_MockObject_MockObject */
  35. private $contactsStore;
  36. /** @var IAppManager|PHPUnit_Framework_MockObject_MockObject */
  37. private $appManager;
  38. /** @var ActionProviderStore|PHPUnit_Framework_MockObject_MockObject */
  39. private $actionProviderStore;
  40. /** @var Manager */
  41. private $manager;
  42. protected function setUp() {
  43. parent::setUp();
  44. $this->contactsStore = $this->createMock(ContactsStore::class);
  45. $this->actionProviderStore = $this->createMock(ActionProviderStore::class);
  46. $this->appManager = $this->createMock(IAppManager::class);
  47. $this->manager = new Manager($this->contactsStore, $this->actionProviderStore, $this->appManager);
  48. }
  49. private function generateTestEntries() {
  50. $entries = [];
  51. foreach (range('Z', 'A') as $char) {
  52. $entry = $this->createMock(IEntry::class);
  53. $entry->expects($this->any())
  54. ->method('getFullName')
  55. ->willReturn('Contact ' . $char);
  56. $entries[] = $entry;
  57. }
  58. return $entries;
  59. }
  60. public function testGetFilteredEntries() {
  61. $filter = 'con';
  62. $user = $this->createMock(IUser::class);
  63. $entries = $this->generateTestEntries();
  64. $provider = $this->createMock(IProvider::class);
  65. $this->contactsStore->expects($this->once())
  66. ->method('getContacts')
  67. ->with($user, $filter)
  68. ->willReturn($entries);
  69. $this->actionProviderStore->expects($this->once())
  70. ->method('getProviders')
  71. ->with($user)
  72. ->willReturn([$provider]);
  73. $provider->expects($this->exactly(25))
  74. ->method('process');
  75. $this->appManager->expects($this->once())
  76. ->method('isEnabledForUser')
  77. ->with($this->equalTo('contacts'), $user)
  78. ->willReturn(false);
  79. $expected = [
  80. 'contacts' => array_slice($entries, 0, 25),
  81. 'contactsAppEnabled' => false,
  82. ];
  83. $data = $this->manager->getEntries($user, $filter);
  84. $this->assertEquals($expected, $data);
  85. }
  86. public function testFindOne() {
  87. $shareTypeFilter = 42;
  88. $shareWithFilter = 'foobar';
  89. $user = $this->createMock(IUser::class);
  90. $entry = current($this->generateTestEntries());
  91. $provider = $this->createMock(IProvider::class);
  92. $this->contactsStore->expects($this->once())
  93. ->method('findOne')
  94. ->with($user, $shareTypeFilter, $shareWithFilter)
  95. ->willReturn($entry);
  96. $this->actionProviderStore->expects($this->once())
  97. ->method('getProviders')
  98. ->with($user)
  99. ->willReturn([$provider]);
  100. $provider->expects($this->once())
  101. ->method('process');
  102. $data = $this->manager->findOne($user, $shareTypeFilter, $shareWithFilter);
  103. $this->assertEquals($entry, $data);
  104. }
  105. public function testFindOne404() {
  106. $shareTypeFilter = 42;
  107. $shareWithFilter = 'foobar';
  108. $user = $this->createMock(IUser::class);
  109. $provider = $this->createMock(IProvider::class);
  110. $this->contactsStore->expects($this->once())
  111. ->method('findOne')
  112. ->with($user, $shareTypeFilter, $shareWithFilter)
  113. ->willReturn(null);
  114. $this->actionProviderStore->expects($this->never())
  115. ->method('getProviders')
  116. ->with($user)
  117. ->willReturn([$provider]);
  118. $provider->expects($this->never())
  119. ->method('process');
  120. $data = $this->manager->findOne($user, $shareTypeFilter, $shareWithFilter);
  121. $this->assertEquals(null, $data);
  122. }
  123. }