X Tutup
Skip to content

Commit 0d750e8

Browse files
committed
Adding AvatarHomeTest
1 parent f57c48f commit 0d750e8

File tree

4 files changed

+147
-9
lines changed

4 files changed

+147
-9
lines changed

apps/dav/lib/Avatars/AvatarHome.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,28 @@
2323
namespace OCA\DAV\Avatars;
2424

2525

26+
use OCP\IAvatarManager;
2627
use Sabre\DAV\Exception\Forbidden;
2728
use Sabre\DAV\Exception\MethodNotAllowed;
2829
use Sabre\DAV\Exception\NotFound;
2930
use Sabre\DAV\ICollection;
3031
use Sabre\HTTP\URLUtil;
3132

3233
class AvatarHome implements ICollection {
34+
35+
/** @var array */
3336
private $principalInfo;
37+
/** @var IAvatarManager */
38+
private $avatarManager;
3439

3540
/**
3641
* AvatarHome constructor.
3742
*
3843
* @param array $principalInfo
3944
*/
40-
public function __construct($principalInfo) {
45+
public function __construct($principalInfo, IAvatarManager $avatarManager) {
4146
$this->principalInfo = $principalInfo;
47+
$this->avatarManager = $avatarManager;
4248
}
4349

4450
function createFile($name, $data = null) {
@@ -59,8 +65,8 @@ function getChild($name) {
5965
if ($size <= 0 || $size > 1024) {
6066
throw new MethodNotAllowed('Invalid image size');
6167
}
62-
$avatar = \OC::$server->getAvatarManager()->getAvatar($this->getName());
63-
if (!$avatar->exists()) {
68+
$avatar = $this->avatarManager->getAvatar($this->getName());
69+
if ($avatar === null || !$avatar->exists()) {
6470
throw new NotFound();
6571
}
6672
return new AvatarNode($size, $ext, $avatar);
@@ -77,8 +83,14 @@ function getChildren() {
7783
}
7884

7985
function childExists($name) {
80-
$ret = $this->getChild($name);
81-
return !is_null($ret);
86+
try {
87+
$ret = $this->getChild($name);
88+
return !is_null($ret);
89+
} catch (NotFound $ex) {
90+
return false;
91+
} catch (MethodNotAllowed $ex) {
92+
return false;
93+
}
8294
}
8395

8496
function delete() {

apps/dav/lib/Avatars/RootCollection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ class RootCollection extends AbstractPrincipalCollection {
1515
* supplied by the authentication backend.
1616
*
1717
* @param array $principalInfo
18-
* @return IPrincipal
18+
* @return AvatarHome
1919
*/
2020
function getChildForPrincipal(array $principalInfo) {
21-
return new AvatarHome($principalInfo);
21+
$avatarManager = \OC::$server->getAvatarManager();
22+
return new AvatarHome($principalInfo, $avatarManager);
2223
}
2324

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

apps/dav/tests/unit/phpunit.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<!-- filters for code coverage -->
1212
<filter>
1313
<whitelist>
14-
<directory suffix=".php">../../dav</directory>
14+
<directory suffix=".php">../../../dav</directory>
1515
<exclude>
16-
<directory suffix=".php">../../dav/tests</directory>
16+
<directory suffix=".php">../../../dav/tests</directory>
1717
</exclude>
1818
</whitelist>
1919
</filter>

0 commit comments

Comments
 (0)
X Tutup