summaryrefslogtreecommitdiffstats
path: root/tests/lib/avatartest.php
blob: 49e8be98c83e13c2850ba95c9aecd9db2b96c768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

/**
 * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

use OC\Avatar;
use OCP\Files\Folder;

class AvatarTest extends \Test\TestCase {
	/** @var  Folder */
	private $folder;

	/** @var  \OC\Avatar */
	private $avatar;

	public function setUp() {
		parent::setUp();

		$this->folder = $this->getMock('\OCP\Files\Folder');
		$l = $this->getMock('\OCP\IL10N');
		$l->method('t')->will($this->returnArgument(0));
		$this->avatar = new \OC\Avatar($this->folder, $l);

	}

	public function testGetNoAvatar() {
		$this->assertEquals(false, $this->avatar->get());
	}

	public function testGetAvatarSizeMatch() {
		$this->folder->method('nodeExists')
			->will($this->returnValueMap([
				['avatar.jpg', true],
				['avatar.128.jpg', true],
			]));

		$expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');

		$file = $this->getMock('\OCP\Files\File');
		$file->method('getContent')->willReturn($expected->data());
		$this->folder->method('get')->with('avatar.128.jpg')->willReturn($file);

		$this->assertEquals($expected->data(), $this->avatar->get(128)->data());
	}

	public function testGetAvatarNoSizeMatch() {
		$this->folder->method('nodeExists')
			->will($this->returnValueMap([
				['avatar.png', true],
				['avatar.32.png', false],
			]));

		$expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
		$expected2 = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
		$expected2->resize(32);

		$file = $this->getMock('\OCP\Files\File');
		$file->method('getContent')->willReturn($expected->data());
		$this->folder->method('get')->with('avatar.png')->willReturn($file);

		$newFile = $this->getMock('\OCP\Files\File');
		$newFile->expects($this->once())
			->method('putContent')
			->with($expected2->data());
		$this->folder->expects($this->once())
			->method('newFile')
			->with('avatar.32.png')
			->willReturn($newFile);

		$this->assertEquals($expected2->data(), $this->avatar->get(32)->data());
	}

	public function testExistsNo() {
		$this->assertFalse($this->avatar->exists());
	}

	public function testExiststJPG() {
		$this->folder->method('nodeExists')
			->will($this->returnValueMap([
				['avatar.jpg', true],
				['avatar.png', false],
			]));
		$this->assertTrue($this->avatar->exists());
	}

	public function testExistsPNG() {
		$this->folder->method('nodeExists')
			->will($this->returnValueMap([
				['avatar.jpg', false],
				['avatar.png', true],
			]));
		$this->assertTrue($this->avatar->exists());
	}

	public function testSetAvatar() {
		$oldFile = $this->getMock('\OCP\Files\File');
		$this->folder->method('get')
			->will($this->returnValueMap([
				['avatar.jpg', $oldFile],
				['avatar.png', $oldFile],
			]));
		$oldFile->expects($this->exactly(2))->method('delete');

		$newFile = $this->getMock('\OCP\Files\File');
		$this->folder->expects($this->once())
			->method('newFile')
			->with('avatar.png')
			->willReturn($newFile);

		$image = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
		$newFile->expects($this->once())
			->method('putContent')
			->with($image->data());

		$this->avatar->set($image->data());
	}

}