AvatarHomeTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Tests\Unit\Avatars;
  26. use OCA\DAV\Avatars\AvatarHome;
  27. use OCA\DAV\Avatars\AvatarNode;
  28. use OCP\IAvatar;
  29. use OCP\IAvatarManager;
  30. use Sabre\DAV\Exception\MethodNotAllowed;
  31. use Sabre\DAV\Exception\NotFound;
  32. use Test\TestCase;
  33. class AvatarHomeTest extends TestCase {
  34. /** @var AvatarHome */
  35. private $home;
  36. /** @var IAvatarManager | \PHPUnit\Framework\MockObject\MockObject */
  37. private $avatarManager;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->avatarManager = $this->createMock(IAvatarManager::class);
  41. $this->home = new AvatarHome(['uri' => 'principals/users/admin'], $this->avatarManager);
  42. }
  43. /**
  44. * @dataProvider providesForbiddenMethods
  45. */
  46. public function testForbiddenMethods($method): void {
  47. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  48. $this->home->$method('');
  49. }
  50. public function providesForbiddenMethods() {
  51. return [
  52. ['createFile'],
  53. ['createDirectory'],
  54. ['delete'],
  55. ['setName']
  56. ];
  57. }
  58. public function testGetName(): void {
  59. $n = $this->home->getName();
  60. self::assertEquals('admin', $n);
  61. }
  62. public function providesTestGetChild() {
  63. return [
  64. [MethodNotAllowed::class, false, ''],
  65. [MethodNotAllowed::class, false, 'bla.foo'],
  66. [MethodNotAllowed::class, false, 'bla.png'],
  67. [NotFound::class, false, '512.png'],
  68. [null, true, '512.png'],
  69. ];
  70. }
  71. /**
  72. * @dataProvider providesTestGetChild
  73. */
  74. public function testGetChild($expectedException, $hasAvatar, $path): void {
  75. if ($expectedException !== null) {
  76. $this->expectException($expectedException);
  77. }
  78. $avatar = $this->createMock(IAvatar::class);
  79. $avatar->method('exists')->willReturn($hasAvatar);
  80. $this->avatarManager->expects($this->any())->method('getAvatar')->with('admin')->willReturn($avatar);
  81. $avatarNode = $this->home->getChild($path);
  82. $this->assertInstanceOf(AvatarNode::class, $avatarNode);
  83. }
  84. public function testGetChildren(): void {
  85. $avatarNodes = $this->home->getChildren();
  86. self::assertEquals(0, count($avatarNodes));
  87. $avatar = $this->createMock(IAvatar::class);
  88. $avatar->expects($this->once())->method('exists')->willReturn(true);
  89. $this->avatarManager->expects($this->any())->method('getAvatar')->with('admin')->willReturn($avatar);
  90. $avatarNodes = $this->home->getChildren();
  91. self::assertEquals(1, count($avatarNodes));
  92. }
  93. /**
  94. * @dataProvider providesTestGetChild
  95. */
  96. public function testChildExists($expectedException, $hasAvatar, $path): void {
  97. $avatar = $this->createMock(IAvatar::class);
  98. $avatar->method('exists')->willReturn($hasAvatar);
  99. $this->avatarManager->expects($this->any())->method('getAvatar')->with('admin')->willReturn($avatar);
  100. $childExists = $this->home->childExists($path);
  101. $this->assertEquals($hasAvatar, $childExists);
  102. }
  103. public function testGetLastModified(): void {
  104. self::assertNull($this->home->getLastModified());
  105. }
  106. }