2019-11-21 07:01:10 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
2019-11-28 08:16:50 +08:00
|
|
|
* For detailed copyright and license information, please view the
|
|
|
|
* LICENSE file that was distributed with this source code.
|
2019-11-21 07:01:10 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Flarum\Tests\unit\User;
|
|
|
|
|
2019-11-23 03:58:58 +08:00
|
|
|
use Flarum\Tests\unit\TestCase;
|
2019-11-21 07:01:10 +08:00
|
|
|
use Flarum\User\AvatarUploader;
|
|
|
|
use Flarum\User\User;
|
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Intervention\Image\ImageManagerStatic;
|
|
|
|
use League\Flysystem\FilesystemInterface;
|
|
|
|
use Mockery as m;
|
|
|
|
|
|
|
|
class AvatarUploaderTest extends TestCase
|
|
|
|
{
|
|
|
|
private $dispatcher;
|
|
|
|
private $filesystem;
|
|
|
|
private $uploader;
|
|
|
|
|
2021-01-07 11:16:26 +08:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-03-28 08:26:26 +08:00
|
|
|
protected function setUp(): void
|
2019-11-21 07:01:10 +08:00
|
|
|
{
|
|
|
|
$this->dispatcher = m::mock(Dispatcher::class);
|
|
|
|
$this->dispatcher->shouldIgnoreMissing();
|
|
|
|
Model::setEventDispatcher($this->dispatcher);
|
|
|
|
|
|
|
|
$this->filesystem = m::mock(FilesystemInterface::class);
|
|
|
|
$this->uploader = new AvatarUploader($this->filesystem);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_removing_avatar_removes_file()
|
|
|
|
{
|
2019-11-23 03:58:58 +08:00
|
|
|
$this->filesystem->shouldReceive('has')->with('ABCDEFGHabcdefgh.png')->andReturn(true);
|
|
|
|
$this->filesystem->shouldReceive('delete')->with('ABCDEFGHabcdefgh.png')->once();
|
2019-11-21 07:01:10 +08:00
|
|
|
|
|
|
|
$user = new User();
|
|
|
|
$user->changeAvatarPath('ABCDEFGHabcdefgh.png');
|
|
|
|
// Necessary because AvatarUpload looks into the original attributes for the raw value,
|
|
|
|
// which isn't usually automatically updated without saving to the database
|
|
|
|
$user->syncOriginal();
|
|
|
|
|
|
|
|
$this->uploader->remove($user);
|
2019-11-23 03:58:58 +08:00
|
|
|
|
|
|
|
// Simulate saving
|
|
|
|
foreach ($user->releaseAfterSaveCallbacks() as $callback) {
|
|
|
|
$callback($user);
|
|
|
|
}
|
2019-11-21 07:01:10 +08:00
|
|
|
$user->syncOriginal();
|
|
|
|
|
2021-03-05 22:43:35 +08:00
|
|
|
$this->assertEquals(null, $user->getRawOriginal('avatar_url'));
|
2019-11-21 07:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_removing_url_avatar_removes_no_file()
|
|
|
|
{
|
2019-11-23 03:58:58 +08:00
|
|
|
$this->filesystem->shouldReceive('has')->with('https://example.com/avatar.png')->andReturn(false)->once();
|
2019-11-21 07:01:10 +08:00
|
|
|
$this->filesystem->shouldNotReceive('delete');
|
|
|
|
|
|
|
|
$user = new User();
|
|
|
|
$user->changeAvatarPath('https://example.com/avatar.png');
|
|
|
|
$user->syncOriginal();
|
|
|
|
|
|
|
|
$this->uploader->remove($user);
|
2019-11-23 03:58:58 +08:00
|
|
|
|
|
|
|
// Simulate saving
|
|
|
|
foreach ($user->releaseAfterSaveCallbacks() as $callback) {
|
|
|
|
$callback($user);
|
|
|
|
}
|
2019-11-21 07:01:10 +08:00
|
|
|
$user->syncOriginal();
|
|
|
|
|
2021-03-05 22:43:35 +08:00
|
|
|
$this->assertEquals(null, $user->getRawOriginal('avatar_url'));
|
2019-11-21 07:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_changing_avatar_removes_file()
|
|
|
|
{
|
2019-11-23 03:58:58 +08:00
|
|
|
$this->filesystem->shouldReceive('put')->once();
|
2019-11-21 07:01:10 +08:00
|
|
|
$this->filesystem->shouldReceive('has')->with('ABCDEFGHabcdefgh.png')->andReturn(true);
|
2019-11-23 03:58:58 +08:00
|
|
|
$this->filesystem->shouldReceive('delete')->with('ABCDEFGHabcdefgh.png')->once();
|
2019-11-21 07:01:10 +08:00
|
|
|
|
|
|
|
$user = new User();
|
|
|
|
$user->changeAvatarPath('ABCDEFGHabcdefgh.png');
|
|
|
|
$user->syncOriginal();
|
|
|
|
|
|
|
|
$this->uploader->upload($user, ImageManagerStatic::canvas(50, 50));
|
2019-11-23 03:58:58 +08:00
|
|
|
|
|
|
|
// Simulate saving
|
|
|
|
foreach ($user->releaseAfterSaveCallbacks() as $callback) {
|
|
|
|
$callback($user);
|
|
|
|
}
|
2019-11-21 07:01:10 +08:00
|
|
|
$user->syncOriginal();
|
|
|
|
|
2021-03-05 22:43:35 +08:00
|
|
|
$this->assertNotEquals('ABCDEFGHabcdefgh.png', $user->getRawOriginal('avatar_url'));
|
2019-11-21 07:01:10 +08:00
|
|
|
}
|
|
|
|
}
|