diff --git a/src/Controllers/ViewProxies/PostViewProxy.php b/src/Controllers/ViewProxies/PostViewProxy.php index 94e27ff0..1bfc7dba 100644 --- a/src/Controllers/ViewProxies/PostViewProxy.php +++ b/src/Controllers/ViewProxies/PostViewProxy.php @@ -6,7 +6,7 @@ use Szurubooru\Helpers\MimeHelper; use Szurubooru\Privilege; use Szurubooru\Services\AuthService; use Szurubooru\Services\FavoritesService; -use Szurubooru\Services\HistoryService; +use Szurubooru\Services\PostHistoryService; use Szurubooru\Services\PostNotesService; use Szurubooru\Services\PrivilegeService; use Szurubooru\Services\ScoreService; @@ -23,7 +23,7 @@ class PostViewProxy extends AbstractViewProxy private $privilegeService; private $authService; - private $historyService; + private $postHistoryService; private $favoritesService; private $scoreService; private $postNotesService; @@ -35,7 +35,7 @@ class PostViewProxy extends AbstractViewProxy public function __construct( PrivilegeService $privilegeService, AuthService $authService, - HistoryService $historyService, + PostHistoryService $postHistoryService, FavoritesService $favoritesService, ScoreService $scoreService, PostNotesService $postNotesService, @@ -46,7 +46,7 @@ class PostViewProxy extends AbstractViewProxy { $this->privilegeService = $privilegeService; $this->authService = $authService; - $this->historyService = $historyService; + $this->postHistoryService = $postHistoryService; $this->favoritesService = $favoritesService; $this->scoreService = $scoreService; $this->postNotesService = $postNotesService; @@ -102,7 +102,7 @@ class PostViewProxy extends AbstractViewProxy if (!empty($config[self::FETCH_HISTORY])) { if ($this->privilegeService->hasPrivilege(Privilege::VIEW_HISTORY)) - $result->history = $this->snapshotViewProxy->fromArray($this->historyService->getPostHistory($post)); + $result->history = $this->snapshotViewProxy->fromArray($this->postHistoryService->getPostHistory($post)); else $result->history = []; } diff --git a/src/Services/HistoryService.php b/src/Services/HistoryService.php index 5d9e70d9..90c2d777 100644 --- a/src/Services/HistoryService.php +++ b/src/Services/HistoryService.php @@ -1,39 +1,26 @@ validator = $validator; $this->snapshotDao = $snapshotDao; - $this->globalParamDao = $globalParamDao; $this->timeService = $timeService; $this->authService = $authService; $this->transactionManager = $transactionManager; @@ -48,23 +35,6 @@ class HistoryService return $this->transactionManager->rollback($transactionFunc); } - public function getPostHistory(Post $post) - { - $filter = new SnapshotFilter(); - - $requirement = new Requirement(); - $requirement->setType(SnapshotFilter::REQUIREMENT_PRIMARY_KEY); - $requirement->setValue(new RequirementSingleValue($post->getId())); - $filter->addRequirement($requirement); - - $requirement = new Requirement(); - $requirement->setType(SnapshotFilter::REQUIREMENT_TYPE); - $requirement->setValue(new RequirementSingleValue(Snapshot::TYPE_POST)); - $filter->addRequirement($requirement); - - return $this->getFiltered($filter)->getEntities(); - } - public function saveSnapshot(Snapshot $snapshot) { $transactionFunc = function() use ($snapshot) @@ -92,57 +62,6 @@ class HistoryService return $this->transactionManager->commit($transactionFunc); } - public function getPostDeleteSnapshot(Post $post) - { - $snapshot = $this->getPostSnapshot($post); - $snapshot->setData([]); - $snapshot->setOperation(Snapshot::OPERATION_DELETE); - return $snapshot; - } - - public function getPostChangeSnapshot(Post $post) - { - static $featuredPostParam = null; - if ($featuredPostParam === null) - $featuredPostParam = $this->globalParamDao->findByKey(GlobalParam::KEY_FEATURED_POST); - $isFeatured = ($featuredPostParam and intval($featuredPostParam->getValue()) === $post->getId()); - - $flags = []; - if ($post->getFlags() & Post::FLAG_LOOP) - $flags[] = 'loop'; - - $data = - [ - 'source' => $post->getSource(), - 'safety' => EnumHelper::postSafetyToString($post->getSafety()), - 'contentChecksum' => $post->getContentChecksum(), - 'featured' => $isFeatured, - - 'tags' => - array_map( - function ($tag) - { - return $tag->getName(); - }, - $post->getTags()), - - 'relations' => - array_map( - function ($post) - { - return $post->getId(); - }, - $post->getRelatedPosts()), - - 'flags' => $flags, - ]; - - $snapshot = $this->getPostSnapshot($post); - $snapshot->setOperation(Snapshot::OPERATION_CHANGE); - $snapshot->setData($data); - return $snapshot; - } - public function getSnapshotDataDifference($newData, $oldData) { $diffFunction = function($base, $other) @@ -172,12 +91,4 @@ class HistoryService '-' => $diffFunction($oldData, $newData), ]; } - - private function getPostSnapshot(Post $post) - { - $snapshot = new Snapshot(); - $snapshot->setType(Snapshot::TYPE_POST); - $snapshot->setPrimaryKey($post->getId()); - return $snapshot; - } } diff --git a/src/Services/PostHistoryService.php b/src/Services/PostHistoryService.php new file mode 100644 index 00000000..c6a62e0b --- /dev/null +++ b/src/Services/PostHistoryService.php @@ -0,0 +1,58 @@ +transactionManager = $transactionManager; + $this->historyService = $historyService; + $this->postSnapshotProvider = $postSnapshotProvider; + } + + public function getPostHistory(Post $post) + { + $transactionFunc = function() use ($post) + { + $filter = new SnapshotFilter(); + + $requirement = new Requirement(); + $requirement->setType(SnapshotFilter::REQUIREMENT_PRIMARY_KEY); + $requirement->setValue(new RequirementSingleValue($post->getId())); + $filter->addRequirement($requirement); + + $requirement = new Requirement(); + $requirement->setType(SnapshotFilter::REQUIREMENT_TYPE); + $requirement->setValue(new RequirementSingleValue(Snapshot::TYPE_POST)); + $filter->addRequirement($requirement); + + return $this->historyService->getFiltered($filter)->getEntities(); + }; + return $this->transactionManager->rollback($transactionFunc); + } + + public function savePostChange(Post $post) + { + $this->historyService->saveSnapshot($this->postSnapshotProvider->getPostChangeSnapshot($post)); + } + + public function savePostDeletion(Post $post) + { + $this->historyService->saveSnapshot($this->postSnapshotProvider->getPostDeleteSnapshot($post)); + } +} diff --git a/src/Services/PostNotesService.php b/src/Services/PostNotesService.php index 9040cd34..d270f9c1 100644 --- a/src/Services/PostNotesService.php +++ b/src/Services/PostNotesService.php @@ -5,7 +5,7 @@ use Szurubooru\Dao\PostNoteDao; use Szurubooru\Entities\Post; use Szurubooru\Entities\PostNote; use Szurubooru\FormData\PostNoteFormData; -use Szurubooru\Services\HistoryService; +use Szurubooru\Services\PostHistoryService; use Szurubooru\Validator; class PostNotesService @@ -13,18 +13,18 @@ class PostNotesService private $validator; private $transactionManager; private $postNoteDao; - private $historyService; + private $postHistoryService; public function __construct( Validator $validator, TransactionManager $transactionManager, PostNoteDao $postNoteDao, - HistoryService $historyService) + PostHistoryService $postHistoryService) { $this->validator = $validator; $this->transactionManager = $transactionManager; $this->postNoteDao = $postNoteDao; - $this->historyService = $historyService; + $this->postHistoryService = $postHistoryService; } public function getById($postNoteId) @@ -58,7 +58,7 @@ class PostNotesService $this->updatePostNoteWithFormData($postNote, $formData); $this->postNoteDao->save($postNote); - $this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($post)); + $this->postHistoryService->savePostChange($post); return $postNote; }; return $this->transactionManager->commit($transactionFunc); @@ -71,7 +71,7 @@ class PostNotesService $this->updatePostNoteWithFormData($postNote, $formData); $this->postNoteDao->save($postNote); - $this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($postNote->getPost())); + $this->postHistoryService->savePostChange($postNote->getPost()); return $postNote; }; return $this->transactionManager->commit($transactionFunc); diff --git a/src/Services/PostService.php b/src/Services/PostService.php index 487ea32e..ddc70ce8 100644 --- a/src/Services/PostService.php +++ b/src/Services/PostService.php @@ -17,7 +17,7 @@ use Szurubooru\SearchServices\Requirements\Requirement; use Szurubooru\SearchServices\Requirements\RequirementCompositeValue; use Szurubooru\SearchServices\Requirements\RequirementSingleValue; use Szurubooru\Services\AuthService; -use Szurubooru\Services\HistoryService; +use Szurubooru\Services\PostHistoryService; use Szurubooru\Services\ImageConverter; use Szurubooru\Services\ImageManipulation\ImageManipulator; use Szurubooru\Services\NetworkingService; @@ -36,7 +36,7 @@ class PostService private $authService; private $networkingService; private $tagService; - private $historyService; + private $postHistoryService; private $imageConverter; private $imageManipulator; @@ -50,7 +50,7 @@ class PostService TimeService $timeService, NetworkingService $networkingService, TagService $tagService, - HistoryService $historyService, + PostHistoryService $postHistoryService, ImageConverter $imageConverter, ImageManipulator $imageManipulator) { @@ -63,7 +63,7 @@ class PostService $this->authService = $authService; $this->networkingService = $networkingService; $this->tagService = $tagService; - $this->historyService = $historyService; + $this->postHistoryService = $postHistoryService; $this->imageConverter = $imageConverter; $this->imageManipulator = $imageManipulator; } @@ -103,27 +103,6 @@ class PostService return $this->transactionManager->rollback($transactionFunc); } - public function getHistory(Post $post) - { - $transactionFunc = function() use ($post) - { - $filter = new SnapshotFilter(); - - $requirement = new Requirement(); - $requirement->setType(SnapshotFilter::REQUIREMENT_PRIMARY_KEY); - $requirement->setValue(new RequirementSingleValue($post->getId())); - $filter->addRequirement($requirement); - - $requirement = new Requirement(); - $requirement->setType(SnapshotFilter::REQUIREMENT_TYPE); - $requirement->setValue(new RequirementSingleValue(Snapshot::TYPE_POST)); - $filter->addRequirement($requirement); - - return $this->historyService->getFiltered($filter)->getEntities(); - }; - return $this->transactionManager->rollback($transactionFunc); - } - public function createPost(UploadFormData $formData) { $transactionFunc = function() use ($formData) @@ -144,7 +123,7 @@ class PostService $savedPost = $this->postDao->save($post); - $this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($savedPost)); + $this->postHistoryService->savePostChange($savedPost); return $savedPost; }; $ret = $this->transactionManager->commit($transactionFunc); @@ -185,7 +164,7 @@ class PostService if (count($formData->flags) > 0) $this->updatePostFlags($post, $formData->flags); - $this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($post)); + $this->postHistoryService->savePostChange($post); return $this->postDao->save($post); }; $ret = $this->transactionManager->commit($transactionFunc); @@ -335,7 +314,7 @@ class PostService { $transactionFunc = function() use ($post) { - $this->historyService->saveSnapshot($this->historyService->getPostDeleteSnapshot($post)); + $this->postHistoryService->savePostDeletion($post); $this->postDao->deleteById($post->getId()); }; $this->transactionManager->commit($transactionFunc); diff --git a/src/Services/PostSnapshotProvider.php b/src/Services/PostSnapshotProvider.php new file mode 100644 index 00000000..d850abd2 --- /dev/null +++ b/src/Services/PostSnapshotProvider.php @@ -0,0 +1,76 @@ +globalParamDao = $globalParamDao; + } + + public function getPostChangeSnapshot(Post $post) + { + static $featuredPostParam = null; + if ($featuredPostParam === null) + $featuredPostParam = $this->globalParamDao->findByKey(GlobalParam::KEY_FEATURED_POST); + $isFeatured = ($featuredPostParam and intval($featuredPostParam->getValue()) === $post->getId()); + + $flags = []; + if ($post->getFlags() & Post::FLAG_LOOP) + $flags[] = 'loop'; + + $data = + [ + 'source' => $post->getSource(), + 'safety' => EnumHelper::postSafetyToString($post->getSafety()), + 'contentChecksum' => $post->getContentChecksum(), + 'featured' => $isFeatured, + + 'tags' => + array_map( + function ($tag) + { + return $tag->getName(); + }, + $post->getTags()), + + 'relations' => + array_map( + function ($post) + { + return $post->getId(); + }, + $post->getRelatedPosts()), + + 'flags' => $flags, + ]; + + $snapshot = $this->getPostSnapshot($post); + $snapshot->setOperation(Snapshot::OPERATION_CHANGE); + $snapshot->setData($data); + return $snapshot; + } + + public function getPostDeleteSnapshot(Post $post) + { + $snapshot = $this->getPostSnapshot($post); + $snapshot->setData([]); + $snapshot->setOperation(Snapshot::OPERATION_DELETE); + return $snapshot; + } + + private function getPostSnapshot(Post $post) + { + $snapshot = new Snapshot(); + $snapshot->setType(Snapshot::TYPE_POST); + $snapshot->setPrimaryKey($post->getId()); + return $snapshot; + } +} diff --git a/tests/Services/HistoryServiceTest.php b/tests/Services/HistoryServiceTest.php index b1c33550..c372d126 100644 --- a/tests/Services/HistoryServiceTest.php +++ b/tests/Services/HistoryServiceTest.php @@ -1,23 +1,15 @@ validatorMock = $this->mock(Validator::class); $this->snapshotDaoMock = $this->mock(SnapshotDao::class); - $this->globalParamDaoMock = $this->mock(GlobalParamDao::class); $this->transactionManagerMock = $this->mock(TransactionManager::class); $this->timeServiceMock = $this->mock(TimeService::class); $this->authServiceMock = $this->mock(AuthService::class); } - public function testPostChangeSnapshot() - { - $tag1 = new Tag(); - $tag2 = new Tag(); - $tag1->setName('tag1'); - $tag2->setName('tag2'); - $post1 = new Post(1); - $post2 = new Post(2); - - $post = new Post(5); - $post->setTags([$tag1, $tag2]); - $post->setRelatedPosts([$post1, $post2]); - $post->setContentChecksum('checksum'); - $post->setSafety(Post::POST_SAFETY_SKETCHY); - $post->setSource('amazing source'); - $post->setFlags(Post::FLAG_LOOP); - - $historyService = $this->getHistoryService(); - $snapshot = $historyService->getPostChangeSnapshot($post); - - $this->assertEquals([ - 'source' => 'amazing source', - 'safety' => 'sketchy', - 'contentChecksum' => 'checksum', - 'featured' => false, - 'tags' => ['tag1', 'tag2'], - 'relations' => [1, 2], - 'flags' => ['loop'], - ], $snapshot->getData()); - - $this->assertEquals(Snapshot::TYPE_POST, $snapshot->getType()); - $this->assertEquals(5, $snapshot->getPrimaryKey()); - - return $post; - } - - /** - * @depends testPostChangeSnapshot - */ - public function testPostChangeSnapshotFeature($post) - { - $param = new GlobalParam; - $param->setValue($post->getId()); - $this->globalParamDaoMock - ->expects($this->once()) - ->method('findByKey') - ->with(GlobalParam::KEY_FEATURED_POST) - ->willReturn($param); - - $historyService = $this->getHistoryService(); - $snapshot = $historyService->getPostChangeSnapshot($post); - - $this->assertTrue($snapshot->getData()['featured']); - } - /** * @dataProvider snapshotDataDifferenceProvider */ @@ -187,9 +122,7 @@ final class HistoryServiceTest extends AbstractTestCase private function getHistoryService() { return new HistoryService( - $this->validatorMock, $this->snapshotDaoMock, - $this->globalParamDaoMock, $this->transactionManagerMock, $this->timeServiceMock, $this->authServiceMock); diff --git a/tests/Services/PostServiceTest.php b/tests/Services/PostServiceTest.php index 6eb296a6..7ee22841 100644 --- a/tests/Services/PostServiceTest.php +++ b/tests/Services/PostServiceTest.php @@ -8,7 +8,7 @@ use Szurubooru\Entities\User; use Szurubooru\FormData\UploadFormData; use Szurubooru\Injector; use Szurubooru\Services\AuthService; -use Szurubooru\Services\HistoryService; +use Szurubooru\Services\PostHistoryService; use Szurubooru\Services\ImageConverter; use Szurubooru\Services\ImageManipulation\ImageManipulator; use Szurubooru\Services\NetworkingService; @@ -29,7 +29,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase private $timeServiceMock; private $networkingServiceMock; private $tagService; - private $historyServiceMock; + private $postHistoryServiceMock; private $imageConverterMock; private $imageManipulatorMock; @@ -45,7 +45,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase $this->timeServiceMock = $this->mock(TimeService::class); $this->networkingServiceMock = $this->mock(NetworkingService::class); $this->tagService = Injector::get(TagService::class); - $this->historyServiceMock = $this->mock(HistoryService::class); + $this->postHistoryServiceMock = $this->mock(PostHistoryService::class); $this->configMock->set('database/maxPostSize', 1000000); $this->imageConverterMock = $this->mock(ImageConverter::class); $this->imageManipulatorMock = $this->mock(ImageManipulator::class); @@ -61,7 +61,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->authServiceMock->expects($this->once())->method('getLoggedInUser')->willReturn(new User(5)); - $this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); + $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot()); $this->imageConverterMock->expects($this->never())->method('createImageFromBuffer'); $this->postService = $this->getPostService(); @@ -91,7 +91,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->imageManipulatorMock->expects($this->once())->method('getImageWidth')->willReturn(640); $this->imageManipulatorMock->expects($this->once())->method('getImageHeight')->willReturn(480); - $this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); + $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot()); $this->postService = $this->getPostService(); $savedPost = $this->postService->createPost($formData); @@ -112,7 +112,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase $formData->contentFileName = 'blah'; $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); - $this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); + $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot()); $this->imageConverterMock->expects($this->once())->method('createImageFromBuffer'); $this->postService = $this->getPostService(); @@ -132,7 +132,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase $formData->contentFileName = 'blah'; $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); - $this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); + $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot()); $this->imageConverterMock->expects($this->once())->method('createImageFromBuffer'); $this->postService = $this->getPostService(); @@ -196,7 +196,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->authServiceMock->expects($this->never())->method('getLoggedInUser'); - $this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); + $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot()); $this->postService = $this->getPostService(); $savedPost = $this->postService->createPost($formData); @@ -215,7 +215,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase $this->timeServiceMock, $this->networkingServiceMock, $this->tagService, - $this->historyServiceMock, + $this->postHistoryServiceMock, $this->imageConverterMock, $this->imageManipulatorMock); } diff --git a/tests/Services/PostSnapshotProviderTest.php b/tests/Services/PostSnapshotProviderTest.php new file mode 100644 index 00000000..62af16b5 --- /dev/null +++ b/tests/Services/PostSnapshotProviderTest.php @@ -0,0 +1,80 @@ +globalParamDaoMock = $this->mock(GlobalParamDao::class); + } + + public function testPostChangeSnapshot() + { + $tag1 = new Tag(); + $tag2 = new Tag(); + $tag1->setName('tag1'); + $tag2->setName('tag2'); + $post1 = new Post(1); + $post2 = new Post(2); + + $post = new Post(5); + $post->setTags([$tag1, $tag2]); + $post->setRelatedPosts([$post1, $post2]); + $post->setContentChecksum('checksum'); + $post->setSafety(Post::POST_SAFETY_SKETCHY); + $post->setSource('amazing source'); + $post->setFlags(Post::FLAG_LOOP); + + $postSnapshotProvider = $this->getPostSnapshotProvider(); + $snapshot = $postSnapshotProvider->getPostChangeSnapshot($post); + + $this->assertEquals([ + 'source' => 'amazing source', + 'safety' => 'sketchy', + 'contentChecksum' => 'checksum', + 'featured' => false, + 'tags' => ['tag1', 'tag2'], + 'relations' => [1, 2], + 'flags' => ['loop'], + ], $snapshot->getData()); + + $this->assertEquals(Snapshot::TYPE_POST, $snapshot->getType()); + $this->assertEquals(5, $snapshot->getPrimaryKey()); + + return $post; + } + + /** + * @depends testPostChangeSnapshot + */ + public function testPostChangeSnapshotFeature($post) + { + $param = new GlobalParam; + $param->setValue($post->getId()); + $this->globalParamDaoMock + ->expects($this->once()) + ->method('findByKey') + ->with(GlobalParam::KEY_FEATURED_POST) + ->willReturn($param); + + $postSnapshotProvider = $this->getPostSnapshotProvider(); + $snapshot = $postSnapshotProvider->getPostChangeSnapshot($post); + + $this->assertTrue($snapshot->getData()['featured']); + } + + private function getPostSnapshotProvider() + { + return new PostSnapshotProvider($this->globalParamDaoMock); + } +}