|
20 | 20 | use OCP\IDBConnection; |
21 | 21 | use OCP\IUser; |
22 | 22 | use OCP\IUserManager; |
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
23 | 24 | use Psr\Http\Client\ClientExceptionInterface; |
24 | 25 | use Psr\Log\LoggerInterface; |
25 | 26 | use Psr\Log\NullLogger; |
|
28 | 29 |
|
29 | 30 | class SyncServiceTest extends TestCase { |
30 | 31 |
|
31 | | - protected CardDavBackend $backend; |
32 | | - protected IUserManager $userManager; |
33 | | - protected IDBConnection $dbConnection; |
| 32 | + protected CardDavBackend&MockObject $backend; |
| 33 | + protected IUserManager&MockObject $userManager; |
| 34 | + protected IDBConnection&MockObject $dbConnection; |
34 | 35 | protected LoggerInterface $logger; |
35 | | - protected Converter $converter; |
36 | | - protected IClient $client; |
37 | | - protected IConfig $config; |
| 36 | + protected Converter&MockObject $converter; |
| 37 | + protected IClient&MockObject $client; |
| 38 | + protected IConfig&MockObject $config; |
38 | 39 | protected SyncService $service; |
| 40 | + |
39 | 41 | public function setUp(): void { |
40 | 42 | $addressBook = [ |
41 | 43 | 'id' => 1, |
@@ -487,4 +489,153 @@ public function providerUseAbsoluteUriReport(): array { |
487 | 489 | ['https://server.internal:8080/nextcloud/', 'https://server.internal:8080/nextcloud/remote.php/dav/addressbooks/system/system/system'], |
488 | 490 | ]; |
489 | 491 | } |
| 492 | + |
| 493 | + public function testSyncInstance(): void { |
| 494 | + |
| 495 | + $this->config->method('getAppValue') |
| 496 | + ->willReturnMap([ |
| 497 | + ['dav', 'system_addressbook_exposed', 'yes', 'yes'], |
| 498 | + ['dav', 'system_addressbook_limit', '5000', '5000'] |
| 499 | + ]); |
| 500 | + |
| 501 | + $this->userManager->method('countUsersTotal') |
| 502 | + ->willReturn(4999); |
| 503 | + |
| 504 | + $this->userManager->method('callForAllUsers') |
| 505 | + ->willReturnCallback(function ($callback) { |
| 506 | + $user = $this->createMock(IUser::class); |
| 507 | + $user->method('getBackendClassName')->willReturn('unittest'); |
| 508 | + $user->method('getUID')->willReturn('test-user'); |
| 509 | + $user->method('isEnabled')->willReturn(true); |
| 510 | + $callback($user); |
| 511 | + }); |
| 512 | + |
| 513 | + $this->backend->method('getCards') |
| 514 | + ->willReturn([ |
| 515 | + ['carddata' => "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:test-user\r\nFN:test-user\r\nEND:VCARD\r\n", 'uri' => 'unittest:test-user.vcf'] |
| 516 | + ]); |
| 517 | + |
| 518 | + $clientService = $this->createMock(IClientService::class); |
| 519 | + |
| 520 | + $service = $this->getMockBuilder(SyncService::class) |
| 521 | + ->setConstructorArgs([ |
| 522 | + $this->backend, |
| 523 | + $this->userManager, |
| 524 | + $this->dbConnection, |
| 525 | + $this->logger, |
| 526 | + $this->converter, |
| 527 | + $clientService, |
| 528 | + $this->config |
| 529 | + ]) |
| 530 | + ->onlyMethods(['ensureSystemAddressBookExists', 'updateUser']) |
| 531 | + ->getMock(); |
| 532 | + |
| 533 | + $service->expects($this->once()) |
| 534 | + ->method('ensureSystemAddressBookExists') |
| 535 | + ->willReturn([ |
| 536 | + 'id' => 1, |
| 537 | + 'uri' => 'system', |
| 538 | + 'principaluri' => 'principals/system/system', |
| 539 | + '{DAV:}displayname' => 'system', |
| 540 | + ]); |
| 541 | + $service->expects($this->any()) |
| 542 | + ->method('updateUser'); |
| 543 | + |
| 544 | + $service->syncInstance(); |
| 545 | + } |
| 546 | + |
| 547 | + public function testSyncInstanceWithProgressCallback(): void { |
| 548 | + $this->config->method('getAppValue') |
| 549 | + ->willReturnMap([ |
| 550 | + ['dav', 'system_addressbook_exposed', 'yes', 'yes'], |
| 551 | + ['dav', 'system_addressbook_limit', '5000', '5000'] |
| 552 | + ]); |
| 553 | + |
| 554 | + $this->userManager->method('countUsersTotal') |
| 555 | + ->willReturn(4999); |
| 556 | + |
| 557 | + $this->userManager->method('callForAllUsers') |
| 558 | + ->willReturnCallback(function ($callback) { |
| 559 | + $user = $this->createMock(IUser::class); |
| 560 | + $user->method('getBackendClassName')->willReturn('unittest'); |
| 561 | + $user->method('getUID')->willReturn('test-user'); |
| 562 | + $user->method('isEnabled')->willReturn(true); |
| 563 | + $callback($user); |
| 564 | + }); |
| 565 | + |
| 566 | + $this->backend->method('getCards') |
| 567 | + ->willReturn([ |
| 568 | + ['carddata' => "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:test-user\r\nFN:test-user\r\nEND:VCARD\r\n", 'uri' => 'unittest:test-user.vcf'] |
| 569 | + ]); |
| 570 | + |
| 571 | + $clientService = $this->createMock(IClientService::class); |
| 572 | + |
| 573 | + $service = $this->getMockBuilder(SyncService::class) |
| 574 | + ->setConstructorArgs([ |
| 575 | + $this->backend, |
| 576 | + $this->userManager, |
| 577 | + $this->dbConnection, |
| 578 | + $this->logger, |
| 579 | + $this->converter, |
| 580 | + $clientService, |
| 581 | + $this->config |
| 582 | + ]) |
| 583 | + ->onlyMethods(['ensureSystemAddressBookExists', 'updateUser']) |
| 584 | + ->getMock(); |
| 585 | + |
| 586 | + $service->expects($this->once()) |
| 587 | + ->method('ensureSystemAddressBookExists') |
| 588 | + ->willReturn([ |
| 589 | + 'id' => 1, |
| 590 | + 'uri' => 'system', |
| 591 | + 'principaluri' => 'principals/system/system', |
| 592 | + '{DAV:}displayname' => 'system', |
| 593 | + ]); |
| 594 | + $service->expects($this->any()) |
| 595 | + ->method('updateUser'); |
| 596 | + |
| 597 | + $progressCalled = false; |
| 598 | + $progressCallback = function () use (&$progressCalled) { |
| 599 | + $progressCalled = true; |
| 600 | + }; |
| 601 | + |
| 602 | + $service->syncInstance($progressCallback); |
| 603 | + |
| 604 | + $this->assertTrue($progressCalled); |
| 605 | + } |
| 606 | + |
| 607 | + public function testSyncInstanceWithUserLimitExceeded(): void { |
| 608 | + $this->config->method('getAppValue') |
| 609 | + ->willReturnMap([ |
| 610 | + ['dav', 'system_addressbook_exposed', 'yes', 'yes'], |
| 611 | + ['dav', 'system_addressbook_limit', '5000', '5000'] |
| 612 | + ]); |
| 613 | + |
| 614 | + $this->userManager->method('countUsersTotal') |
| 615 | + ->willReturn(5000); |
| 616 | + |
| 617 | + $this->config->expects($this->once()) |
| 618 | + ->method('setAppValue') |
| 619 | + ->with('dav', 'system_addressbook_exposed', 'no'); |
| 620 | + |
| 621 | + $clientService = $this->createMock(IClientService::class); |
| 622 | + |
| 623 | + $service = $this->getMockBuilder(SyncService::class) |
| 624 | + ->setConstructorArgs([ |
| 625 | + $this->backend, |
| 626 | + $this->userManager, |
| 627 | + $this->dbConnection, |
| 628 | + $this->logger, |
| 629 | + $this->converter, |
| 630 | + $clientService, |
| 631 | + $this->config |
| 632 | + ]) |
| 633 | + ->onlyMethods(['getLocalSystemAddressBook']) |
| 634 | + ->getMock(); |
| 635 | + |
| 636 | + $service->expects($this->never()) |
| 637 | + ->method('getLocalSystemAddressBook'); |
| 638 | + |
| 639 | + $service->syncInstance(); |
| 640 | + } |
490 | 641 | } |
0 commit comments