aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/lib/classes/AvatarClassTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/lib/classes/AvatarClassTest.php')
-rw-r--r--tests/unit/lib/classes/AvatarClassTest.php176
1 files changed, 96 insertions, 80 deletions
diff --git a/tests/unit/lib/classes/AvatarClassTest.php b/tests/unit/lib/classes/AvatarClassTest.php
index 88b24bd..8172e20 100644
--- a/tests/unit/lib/classes/AvatarClassTest.php
+++ b/tests/unit/lib/classes/AvatarClassTest.php
@@ -11,136 +11,152 @@
require_once 'lib/phplib/Seminar_Perm.class.php';
-/**
- * Testcase for Avatar class.
- *
- * @package studip
- * @subpackage test
- *
- * @author mlunzena
- * @copyright (c) Authors
- */
-class AvatarTestCase extends \Codeception\Test\Unit
+abstract class AvatarTest extends \Codeception\Test\Unit
{
- private $avatar_id;
- private $avatar;
+ protected $avatar_id;
+ protected $avatar;
- public function setUp(): void
+ abstract protected function getType(): string;
+
+ protected function createPath(string $avatar_id, ?string $size, bool $subdir): string
{
- $stub = $this->createMock('Seminar_Perm');
- // Configure the stub.
- $stub->expects($this->any())
- ->method('have_perm')
- ->will($this->returnValue(true));
+ $result = $avatar_id;
- $GLOBALS['perm'] = $stub;
- $GLOBALS['DYNAMIC_CONTENT_URL'] = "/dynamic";
- $GLOBALS['DYNAMIC_CONTENT_PATH'] = "/dynamic";
- $this->avatar_id = "123456789";
- $this->avatar = Avatar::getAvatar($this->avatar_id);
+ if ($size) {
+ $result .= "_{$size}";
+ }
+
+ $result .= '.' . Avatar::EXTENSION;
+
+ if ($subdir) {
+ $result = substr($avatar_id, 0, 2) . '/' . $result;
+ }
+ return "/dynamic/{$this->getType()}/{$result}";
}
- public function tearDown(): void
+ protected function createFixedPath(?string $size): string
+ {
+ $result = 'nobody';
+
+ if ($size) {
+ $result .= "_{$size}";
+ }
+
+ $result .= '.' . Avatar::EXTENSION;
+
+ return "/fixed/images/avatars/{$this->getType()}/{$result}";
+ }
+
+ protected function setUp(): void
{
- unset($GLOBALS['DYNAMIC_CONTENT_PATH'], $GLOBALS['DYNAMIC_CONTENT_URL']);
+ $GLOBALS['DYNAMIC_CONTENT_URL'] = "/dynamic";
+ $GLOBALS['DYNAMIC_CONTENT_PATH'] = "/dynamic";
+ $GLOBALS['ASSETS_URL'] = "/fixed/";
+ $GLOBALS['ASSETS_PATH'] = "/fixed/";
+
+ Assets::set_assets_url($GLOBALS['ASSETS_URL']);
+ Assets::set_assets_path($GLOBALS['ASSETS_PATH']);
}
- public function test_class_should_exist()
+ public function tearDown(): void
{
- $this->assertTrue(class_exists('Avatar'));
+ unset(
+ $GLOBALS['DYNAMIC_CONTENT_PATH'],
+ $GLOBALS['DYNAMIC_CONTENT_URL'],
+ $GLOBALS['ASSETS_URL'],
+ $GLOBALS['ASSETS_PATH']
+ );
}
public function test_avatar_url()
{
- $url = $this->avatar->getCustomAvatarUrl(Avatar::NORMAL);
- $this->assertEquals("/dynamic/user/" . $this->avatar_id . "_normal.png?d=0", $url);
+ $this->assertEquals(
+ $this->createPath($this->avatar_id, Avatar::NORMAL, true) . '?d=0',
+ $this->avatar->getCustomAvatarUrl(Avatar::NORMAL)
+ );
}
public function test_avatar_path()
{
- $path = $this->avatar->getCustomAvatarPath(Avatar::NORMAL);
- $this->assertEquals("/dynamic/user/" . $this->avatar_id . "_normal.png", $path);
+ $this->assertEquals(
+ $this->createPath($this->avatar_id, Avatar::NORMAL, true),
+ $this->avatar->getCustomAvatarPath(Avatar::NORMAL)
+ );
}
public function test_nobody_url()
{
- $url = Avatar::getNobody()->getUrl(Avatar::NORMAL);
- $this->assertEquals("/dynamic/user/nobody_normal.png?d=0", $url);
+ $this->assertEquals(
+ $this->createFixedPath(Avatar::NORMAL),
+ $this->avatar->getNobody()->getURL(Avatar::NORMAL)
+ );
}
public function test_nobody_path()
{
- $path = Avatar::getNobody()->getCustomAvatarPath(Avatar::NORMAL);
- $this->assertEquals("/dynamic/user/nobody_normal.png", $path);
+ $this->assertEquals(
+ $this->createFixedPath(Avatar::NORMAL),
+ $this->avatar->getNobody()->getFilename(Avatar::NORMAL)
+ );
}
}
-
-class CourseAvatarTestCase extends \Codeception\Test\Unit
+/**
+ * Testcase for Avatar class.
+ *
+ * @package studip
+ * @subpackage test
+ *
+ * @author mlunzena
+ * @copyright (c) Authors
+ */
+class AvatarTestCase extends AvatarTest
{
- private $avatar_id;
- private $avatar;
-
public function setUp(): void
{
- $this->avatar_id = "123456789";
- $this->avatar = CourseAvatar::getAvatar($this->avatar_id);
-
- $this->setUpFS();
+ parent::setUp();
- $GLOBALS['DYNAMIC_CONTENT_URL'] = "/dynamic";
- $GLOBALS['DYNAMIC_CONTENT_PATH'] = "/dynamic";
- }
+ $stub = $this->createMock('Seminar_Perm');
+ // Configure the stub.
+ $stub->expects($this->any())
+ ->method('have_perm')
+ ->will($this->returnValue(true));
- private function setUpFS()
- {
- ArrayFileStream::set_filesystem([
- 'dynamic' => [
- 'course' => [
- $this->avatar_id . '_normal.png' => '',
- $this->avatar_id . '_medium.png' => '',
- $this->avatar_id . '_small.png' => '',
- ],
- ],
- ]);
-
- if (!stream_wrapper_register("var", "ArrayFileStream")) {
- throw new Exception("Failed to register protocol");
- }
- }
+ $GLOBALS['perm'] = $stub;
- public function tearDown(): void
- {
- stream_wrapper_unregister("var");
- unset($GLOBALS['DYNAMIC_CONTENT_PATH'], $GLOBALS['DYNAMIC_CONTENT_URL']);
+ $this->avatar_id = "123456789";
+ $this->avatar = Avatar::getAvatar($this->avatar_id);
}
public function test_class_should_exist()
{
- $this->assertTrue(class_exists('CourseAvatar'));
+ $this->assertTrue(class_exists(Avatar::class));
}
- public function test_avatar_url()
+ protected function getType(): string
{
- $url = $this->avatar->getCustomAvatarUrl(Avatar::NORMAL);
- $this->assertEquals("/dynamic/course/". $this->avatar_id . "_normal.png?d=0", $url);
+ return 'user';
}
+}
- public function test_avatar_path()
+
+class CourseAvatarTestCase extends AvatarTest
+{
+ public function setUp(): void
{
- $path = $this->avatar->getCustomAvatarPath(Avatar::NORMAL);
- $this->assertEquals("/dynamic/course/". $this->avatar_id . "_normal.png", $path);
+ parent::setUp();
+
+ $this->avatar_id = "123456789";
+ $this->avatar = CourseAvatar::getAvatar($this->avatar_id);
}
- public function test_nobody_url()
+ public function test_class_should_exist()
{
- $url = CourseAvatar::getNobody()->getUrl(Avatar::NORMAL);
- $this->assertEquals("/dynamic/course/nobody_normal.png?d=0", $url);
+ $this->assertTrue(class_exists(CourseAvatar::class));
}
- public function test_nobody_path()
+ protected function getType(): string
{
- $path = CourseAvatar::getNobody()->getCustomAvatarPath(Avatar::NORMAL);
- $this->assertEquals("/dynamic/course/nobody_normal.png", $path);
+ return 'course';
}
}