szurubooru/tests/JobTests/EditPostContentJobTest.php
Marcin Kurczewski 4ba83e6834 Changed job arguments convention back
Restored JobArgs approach. Previous introduction of hierarchic argument
definitions has backfired: it was confusing what class to take arguments
from, the concept of sharing arguments between different jobs was
unintelligible and one never knew where given argument was actually
defined.

This appraoch makes it easier to maintain the arguments list and
simplifies the code a lot.
2014-05-12 00:13:18 +02:00

165 lines
4.3 KiB
PHP

<?php
class EditPostContentJobTest extends AbstractTest
{
public function testFile()
{
$this->prepare();
$this->grantAccess('editPostContent');
$post = $this->uploadFromFile('image.jpg');
$this->assert->doesNotThrow(function() use ($post)
{
PostModel::getById($post->getId());
});
}
public function testFileJpeg()
{
$this->prepare();
$this->grantAccess('editPostContent');
$post = $this->uploadFromFile('image.jpg');
$this->assert->areEqual('image/jpeg', $post->getMimeType());
$this->assert->areEqual(PostType::Image, $post->getType()->toInteger());
$this->assert->areEqual(320, $post->getImageWidth());
$this->assert->areEqual(240, $post->getImageHeight());
$this->assert->doesNotThrow(function() use ($post)
{
$post->generateThumb();
});
}
public function testFilePng()
{
$this->prepare();
$this->grantAccess('editPostContent');
$post = $this->uploadFromFile('image.png');
$this->assert->areEqual('image/png', $post->getMimeType());
$this->assert->areEqual(PostType::Image, $post->getType()->toInteger());
$this->assert->areEqual(320, $post->getImageWidth());
$this->assert->areEqual(240, $post->getImageHeight());
$this->assert->doesNotThrow(function() use ($post)
{
$post->generateThumb();
});
}
public function testFileGif()
{
$this->prepare();
$this->grantAccess('editPostContent');
$post = $this->uploadFromFile('image.gif');
$this->assert->areEqual('image/gif', $post->getMimeType());
$this->assert->areEqual(PostType::Image, $post->getType()->toInteger());
$this->assert->areEqual(320, $post->getImageWidth());
$this->assert->areEqual(240, $post->getImageHeight());
$this->assert->doesNotThrow(function() use ($post)
{
$post->generateThumb();
});
}
public function testFileInvalid()
{
$this->prepare();
$this->grantAccess('editPostContent');
$this->assert->throws(function()
{
$this->uploadFromFile('text.txt');
}, 'Invalid file type');
}
public function testUrl()
{
$this->prepare();
$this->grantAccess('editPostContent');
$post = $this->uploadFromUrl('image.jpg');
$this->assert->doesNotThrow(function() use ($post)
{
PostModel::getById($post->getId());
});
}
public function testUrlYoutube()
{
$this->prepare();
$this->grantAccess('editPostContent');
$post = $this->mockPost(Auth::getCurrentUser());
$post = Api::run(
new EditPostContentJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_POST_CONTENT_URL => 'http://www.youtube.com/watch?v=qWq_jydCUw4', 'test.jpg',
]);
$this->assert->areEqual(PostType::Youtube, $post->getType()->toInteger());
$this->assert->areEqual('qWq_jydCUw4', $post->getFileHash());
$this->assert->doesNotThrow(function() use ($post)
{
$post->generateThumb();
});
$this->assert->doesNotThrow(function() use ($post)
{
PostModel::getById($post->getId());
});
}
public function testWrongPostId()
{
$this->assert->throws(function()
{
Api::run(
new EditPostContentJob(),
[
JobArgs::ARG_POST_ID => 100,
JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'),
]);
}, 'Invalid post ID');
}
protected function prepare()
{
$this->login($this->mockUser());
}
protected function uploadFromUrl($fileName, $post = null)
{
if ($post === null)
$post = $this->mockPost(Auth::getCurrentUser());
$url = 'http://example.com/mock_' . $fileName;
TransferHelper::mockForDownload($url, $this->getPath($fileName));
$post = Api::run(
new EditPostContentJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_POST_CONTENT_URL => $url,
]);
$this->assert->areEqual(
file_get_contents($this->getPath($fileName)),
file_get_contents(getConfig()->main->filesPath . DS . $post->getName()));
return $post;
}
protected function uploadFromFile($fileName, $post = null)
{
if ($post === null)
$post = $this->mockPost(Auth::getCurrentUser());
$post = Api::run(
new EditPostContentJob(),
[
JobArgs::ARG_POST_ID => $post->getId(),
JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath($fileName), 'test.jpg'),
]);
$this->assert->areEqual(
file_get_contents($this->getPath($fileName)),
file_get_contents(getConfig()->main->filesPath . DS . $post->getName()));
return $post;
}
}