2014-09-15 11:38:24 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Services;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Dao\GlobalParamDao;
|
|
|
|
use Szurubooru\Dao\PostDao;
|
|
|
|
use Szurubooru\Entities\Post;
|
|
|
|
use Szurubooru\Entities\Snapshot;
|
|
|
|
use Szurubooru\Entities\User;
|
|
|
|
use Szurubooru\FormData\UploadFormData;
|
|
|
|
use Szurubooru\Injector;
|
|
|
|
use Szurubooru\Services\AuthService;
|
2014-10-25 23:53:46 +02:00
|
|
|
use Szurubooru\Services\PostHistoryService;
|
2014-10-24 18:25:33 +02:00
|
|
|
use Szurubooru\Services\ImageConverter;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Services\ImageManipulation\ImageManipulator;
|
2014-10-08 19:39:20 +02:00
|
|
|
use Szurubooru\Services\NetworkingService;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Services\PostService;
|
|
|
|
use Szurubooru\Services\TagService;
|
|
|
|
use Szurubooru\Services\TimeService;
|
|
|
|
use Szurubooru\Tests\AbstractDatabaseTestCase;
|
|
|
|
use Szurubooru\Validator;
|
|
|
|
|
|
|
|
final class PostServiceTest extends AbstractDatabaseTestCase
|
2014-09-15 11:38:24 +02:00
|
|
|
{
|
|
|
|
private $configMock;
|
|
|
|
private $validatorMock;
|
|
|
|
private $transactionManagerMock;
|
|
|
|
private $postDaoMock;
|
2014-09-24 23:24:51 +02:00
|
|
|
private $globalParamDaoMock;
|
2014-09-15 11:38:24 +02:00
|
|
|
private $authServiceMock;
|
|
|
|
private $timeServiceMock;
|
2014-10-08 19:39:20 +02:00
|
|
|
private $networkingServiceMock;
|
2014-10-06 21:12:43 +02:00
|
|
|
private $tagService;
|
2014-10-25 23:53:46 +02:00
|
|
|
private $postHistoryServiceMock;
|
2014-10-24 18:25:33 +02:00
|
|
|
private $imageConverterMock;
|
2014-09-20 18:30:48 +02:00
|
|
|
private $imageManipulatorMock;
|
2014-09-15 11:38:24 +02:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2014-10-06 21:12:43 +02:00
|
|
|
parent::setUp();
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->configMock = $this->mockConfig();
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->validatorMock = $this->mock(Validator::class);
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->transactionManagerMock = $this->mockTransactionManager();
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->postDaoMock = $this->mock(PostDao::class);
|
|
|
|
$this->globalParamDaoMock = $this->mock(GlobalParamDao::class);
|
|
|
|
$this->authServiceMock = $this->mock(AuthService::class);
|
|
|
|
$this->timeServiceMock = $this->mock(TimeService::class);
|
2014-10-08 19:39:20 +02:00
|
|
|
$this->networkingServiceMock = $this->mock(NetworkingService::class);
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->tagService = Injector::get(TagService::class);
|
2014-10-25 23:53:46 +02:00
|
|
|
$this->postHistoryServiceMock = $this->mock(PostHistoryService::class);
|
2014-09-16 18:10:01 +02:00
|
|
|
$this->configMock->set('database/maxPostSize', 1000000);
|
2014-10-24 18:25:33 +02:00
|
|
|
$this->imageConverterMock = $this->mock(ImageConverter::class);
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->imageManipulatorMock = $this->mock(ImageManipulator::class);
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatingYoutubePost()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UploadFormData;
|
|
|
|
$formData->safety = Post::POST_SAFETY_SAFE;
|
2014-09-15 11:38:24 +02:00
|
|
|
$formData->source = 'source';
|
|
|
|
$formData->tags = ['test', 'test2'];
|
|
|
|
$formData->url = 'https://www.youtube.com/watch?v=QYK2c4OVG6s';
|
|
|
|
|
|
|
|
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->authServiceMock->expects($this->once())->method('getLoggedInUser')->willReturn(new User(5));
|
2014-10-25 23:53:46 +02:00
|
|
|
$this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
|
2014-10-24 18:25:33 +02:00
|
|
|
$this->imageConverterMock->expects($this->never())->method('createImageFromBuffer');
|
2014-09-15 11:38:24 +02:00
|
|
|
|
|
|
|
$this->postService = $this->getPostService();
|
|
|
|
$savedPost = $this->postService->createPost($formData);
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->assertEquals(Post::POST_SAFETY_SAFE, $savedPost->getSafety());
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->assertEquals(5, $savedPost->getUserId());
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->assertEquals(Post::POST_TYPE_YOUTUBE, $savedPost->getContentType());
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->assertEquals('QYK2c4OVG6s', $savedPost->getContentChecksum());
|
|
|
|
$this->assertEquals('source', $savedPost->getSource());
|
|
|
|
$this->assertNull($savedPost->getImageWidth());
|
|
|
|
$this->assertNull($savedPost->getImageHeight());
|
|
|
|
$this->assertEquals($formData->url, $savedPost->getOriginalFileName());
|
|
|
|
$this->assertNull($savedPost->getOriginalFileSize());
|
2014-09-21 09:35:43 +02:00
|
|
|
$this->assertEquals(2, count($savedPost->getTags()));
|
|
|
|
$this->assertEquals('test', $savedPost->getTags()[0]->getName());
|
|
|
|
$this->assertEquals('test2', $savedPost->getTags()[1]->getName());
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatingPosts()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UploadFormData;
|
|
|
|
$formData->safety = Post::POST_SAFETY_SAFE;
|
2014-09-15 11:38:24 +02:00
|
|
|
$formData->tags = ['test'];
|
|
|
|
$formData->content = $this->getTestFile('image.jpg');
|
|
|
|
$formData->contentFileName = 'blah';
|
|
|
|
|
|
|
|
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
|
2014-09-20 18:30:48 +02:00
|
|
|
$this->imageManipulatorMock->expects($this->once())->method('getImageWidth')->willReturn(640);
|
|
|
|
$this->imageManipulatorMock->expects($this->once())->method('getImageHeight')->willReturn(480);
|
2014-10-25 23:53:46 +02:00
|
|
|
$this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
|
2014-09-15 11:38:24 +02:00
|
|
|
|
|
|
|
$this->postService = $this->getPostService();
|
|
|
|
$savedPost = $this->postService->createPost($formData);
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->assertEquals(Post::POST_TYPE_IMAGE, $savedPost->getContentType());
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->assertEquals('24216edd12328de3a3c55e2f98220ee7613e3be1', $savedPost->getContentChecksum());
|
|
|
|
$this->assertEquals($formData->contentFileName, $savedPost->getOriginalFileName());
|
|
|
|
$this->assertEquals(687645, $savedPost->getOriginalFileSize());
|
2014-09-20 18:30:48 +02:00
|
|
|
$this->assertEquals(640, $savedPost->getImageWidth());
|
|
|
|
$this->assertEquals(480, $savedPost->getImageHeight());
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatingVideos()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UploadFormData;
|
|
|
|
$formData->safety = Post::POST_SAFETY_SAFE;
|
2014-09-15 11:38:24 +02:00
|
|
|
$formData->tags = ['test'];
|
|
|
|
$formData->content = $this->getTestFile('video.mp4');
|
|
|
|
$formData->contentFileName = 'blah';
|
|
|
|
|
|
|
|
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
|
2014-10-25 23:53:46 +02:00
|
|
|
$this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
|
2014-10-24 18:25:33 +02:00
|
|
|
$this->imageConverterMock->expects($this->once())->method('createImageFromBuffer');
|
2014-09-15 11:38:24 +02:00
|
|
|
|
|
|
|
$this->postService = $this->getPostService();
|
|
|
|
$savedPost = $this->postService->createPost($formData);
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->assertEquals(Post::POST_TYPE_VIDEO, $savedPost->getContentType());
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->assertEquals('16dafaa07cda194d03d590529c06c6ec1a5b80b0', $savedPost->getContentChecksum());
|
|
|
|
$this->assertEquals($formData->contentFileName, $savedPost->getOriginalFileName());
|
|
|
|
$this->assertEquals(14667, $savedPost->getOriginalFileSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreatingFlashes()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UploadFormData;
|
|
|
|
$formData->safety = Post::POST_SAFETY_SAFE;
|
2014-09-15 11:38:24 +02:00
|
|
|
$formData->tags = ['test'];
|
|
|
|
$formData->content = $this->getTestFile('flash.swf');
|
|
|
|
$formData->contentFileName = 'blah';
|
|
|
|
|
|
|
|
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
|
2014-10-25 23:53:46 +02:00
|
|
|
$this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
|
2014-10-24 18:25:33 +02:00
|
|
|
$this->imageConverterMock->expects($this->once())->method('createImageFromBuffer');
|
2014-09-15 11:38:24 +02:00
|
|
|
|
|
|
|
$this->postService = $this->getPostService();
|
|
|
|
$savedPost = $this->postService->createPost($formData);
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->assertEquals(Post::POST_TYPE_FLASH, $savedPost->getContentType());
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->assertEquals('d897e044b801d892291b440534c3be3739034f68', $savedPost->getContentChecksum());
|
|
|
|
$this->assertEquals($formData->contentFileName, $savedPost->getOriginalFileName());
|
|
|
|
$this->assertEquals(226172, $savedPost->getOriginalFileSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileDuplicates()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UploadFormData;
|
|
|
|
$formData->safety = Post::POST_SAFETY_SAFE;
|
2014-09-15 11:38:24 +02:00
|
|
|
$formData->tags = ['test'];
|
|
|
|
$formData->content = $this->getTestFile('flash.swf');
|
|
|
|
$formData->contentFileName = 'blah';
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->postDaoMock->expects($this->once())->method('findByContentChecksum')->willReturn(new Post(5));
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->setExpectedException(\Exception::class, 'Duplicate post: @5');
|
|
|
|
|
|
|
|
$this->postService = $this->getPostService();
|
|
|
|
$this->postService->createPost($formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testYoutubeDuplicates()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UploadFormData;
|
|
|
|
$formData->safety = Post::POST_SAFETY_SAFE;
|
2014-09-15 11:38:24 +02:00
|
|
|
$formData->tags = ['test'];
|
|
|
|
$formData->url = 'https://www.youtube.com/watch?v=QYK2c4OVG6s';
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->postDaoMock->expects($this->once())->method('findByContentChecksum')->with('QYK2c4OVG6s')->willReturn(new Post(5));
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->setExpectedException(\Exception::class, 'Duplicate post: @5');
|
|
|
|
|
|
|
|
$this->postService = $this->getPostService();
|
|
|
|
$this->postService->createPost($formData);
|
|
|
|
}
|
|
|
|
|
2014-09-16 18:10:01 +02:00
|
|
|
public function testTooBigUpload()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UploadFormData;
|
|
|
|
$formData->safety = Post::POST_SAFETY_SAFE;
|
2014-09-16 18:10:01 +02:00
|
|
|
$formData->tags = ['test'];
|
|
|
|
$formData->content = 'aa';
|
|
|
|
|
|
|
|
$this->configMock->set('database/maxPostSize', 1);
|
|
|
|
$this->setExpectedException(\Exception::class, 'Upload is too big');
|
|
|
|
|
|
|
|
$this->postService = $this->getPostService();
|
|
|
|
$this->postService->createPost($formData);
|
|
|
|
}
|
2014-09-16 17:07:07 +02:00
|
|
|
|
|
|
|
public function testAnonymousUploads()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UploadFormData;
|
|
|
|
$formData->safety = Post::POST_SAFETY_SAFE;
|
2014-09-16 17:07:07 +02:00
|
|
|
$formData->tags = ['test'];
|
|
|
|
$formData->url = 'https://www.youtube.com/watch?v=QYK2c4OVG6s';
|
|
|
|
$formData->anonymous = true;
|
|
|
|
|
|
|
|
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
|
|
|
|
$this->authServiceMock->expects($this->never())->method('getLoggedInUser');
|
2014-10-25 23:53:46 +02:00
|
|
|
$this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
|
2014-09-16 17:07:07 +02:00
|
|
|
|
|
|
|
$this->postService = $this->getPostService();
|
|
|
|
$savedPost = $this->postService->createPost($formData);
|
|
|
|
$this->assertNull($savedPost->getUserId());
|
|
|
|
}
|
|
|
|
|
2014-09-15 11:38:24 +02:00
|
|
|
private function getPostService()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
return new PostService(
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->configMock,
|
|
|
|
$this->validatorMock,
|
|
|
|
$this->transactionManagerMock,
|
|
|
|
$this->postDaoMock,
|
2014-09-24 23:24:51 +02:00
|
|
|
$this->globalParamDaoMock,
|
2014-09-15 11:38:24 +02:00
|
|
|
$this->authServiceMock,
|
|
|
|
$this->timeServiceMock,
|
2014-10-08 19:39:20 +02:00
|
|
|
$this->networkingServiceMock,
|
2014-10-06 21:12:43 +02:00
|
|
|
$this->tagService,
|
2014-10-25 23:53:46 +02:00
|
|
|
$this->postHistoryServiceMock,
|
2014-10-24 18:25:33 +02:00
|
|
|
$this->imageConverterMock,
|
2014-09-20 18:30:48 +02:00
|
|
|
$this->imageManipulatorMock);
|
2014-09-15 11:38:24 +02:00
|
|
|
}
|
|
|
|
}
|