AvatarHomeTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2017 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\Unit\Avatars;
  8. use OCA\DAV\Avatars\AvatarHome;
  9. use OCA\DAV\Avatars\AvatarNode;
  10. use OCP\IAvatar;
  11. use OCP\IAvatarManager;
  12. use Sabre\DAV\Exception\MethodNotAllowed;
  13. use Sabre\DAV\Exception\NotFound;
  14. use Test\TestCase;
  15. class AvatarHomeTest extends TestCase {
  16. /** @var AvatarHome */
  17. private $home;
  18. /** @var IAvatarManager | \PHPUnit\Framework\MockObject\MockObject */
  19. private $avatarManager;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->avatarManager = $this->createMock(IAvatarManager::class);
  23. $this->home = new AvatarHome(['uri' => 'principals/users/admin'], $this->avatarManager);
  24. }
  25. /**
  26. * @dataProvider providesForbiddenMethods
  27. */
  28. public function testForbiddenMethods($method): void {
  29. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  30. $this->home->$method('');
  31. }
  32. public function providesForbiddenMethods() {
  33. return [
  34. ['createFile'],
  35. ['createDirectory'],
  36. ['delete'],
  37. ['setName']
  38. ];
  39. }
  40. public function testGetName(): void {
  41. $n = $this->home->getName();
  42. self::assertEquals('admin', $n);
  43. }
  44. public function providesTestGetChild() {
  45. return [
  46. [MethodNotAllowed::class, false, ''],
  47. [MethodNotAllowed::class, false, 'bla.foo'],
  48. [MethodNotAllowed::class, false, 'bla.png'],
  49. [NotFound::class, false, '512.png'],
  50. [null, true, '512.png'],
  51. ];
  52. }
  53. /**
  54. * @dataProvider providesTestGetChild
  55. */
  56. public function testGetChild($expectedException, $hasAvatar, $path): void {
  57. if ($expectedException !== null) {
  58. $this->expectException($expectedException);
  59. }
  60. $avatar = $this->createMock(IAvatar::class);
  61. $avatar->method('exists')->willReturn($hasAvatar);
  62. $this->avatarManager->expects($this->any())->method('getAvatar')->with('admin')->willReturn($avatar);
  63. $avatarNode = $this->home->getChild($path);
  64. $this->assertInstanceOf(AvatarNode::class, $avatarNode);
  65. }
  66. public function testGetChildren(): void {
  67. $avatarNodes = $this->home->getChildren();
  68. self::assertEquals(0, count($avatarNodes));
  69. $avatar = $this->createMock(IAvatar::class);
  70. $avatar->expects($this->once())->method('exists')->willReturn(true);
  71. $this->avatarManager->expects($this->any())->method('getAvatar')->with('admin')->willReturn($avatar);
  72. $avatarNodes = $this->home->getChildren();
  73. self::assertEquals(1, count($avatarNodes));
  74. }
  75. /**
  76. * @dataProvider providesTestGetChild
  77. */
  78. public function testChildExists($expectedException, $hasAvatar, $path): void {
  79. $avatar = $this->createMock(IAvatar::class);
  80. $avatar->method('exists')->willReturn($hasAvatar);
  81. $this->avatarManager->expects($this->any())->method('getAvatar')->with('admin')->willReturn($avatar);
  82. $childExists = $this->home->childExists($path);
  83. $this->assertEquals($hasAvatar, $childExists);
  84. }
  85. public function testGetLastModified(): void {
  86. self::assertNull($this->home->getLastModified());
  87. }
  88. }