4ba83e6834
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.
154 lines
3.7 KiB
PHP
154 lines
3.7 KiB
PHP
<?php
|
|
class AddPostJobTest extends AbstractTest
|
|
{
|
|
public function testSaving()
|
|
{
|
|
$this->prepare();
|
|
|
|
$this->grantAccess('addPost');
|
|
$this->grantAccess('addPostSafety');
|
|
$this->grantAccess('addPostTags');
|
|
$this->grantAccess('addPostSource');
|
|
$this->grantAccess('addPostContent');
|
|
|
|
$this->login($this->mockUser());
|
|
|
|
$post = $this->assert->doesNotThrow(function()
|
|
{
|
|
return Api::run(
|
|
new AddPostJob(),
|
|
[
|
|
JobArgs::ARG_NEW_SAFETY => PostSafety::Safe,
|
|
JobArgs::ARG_NEW_SOURCE => '',
|
|
JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'),
|
|
JobArgs::ARG_NEW_TAG_NAMES => ['kamen', 'raider'],
|
|
]);
|
|
});
|
|
|
|
$this->assert->areEqual(
|
|
file_get_contents($post->getFullPath()),
|
|
file_get_contents($this->getPath('image.jpg')));
|
|
$this->assert->areEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId());
|
|
}
|
|
|
|
public function testAnonymousUploads()
|
|
{
|
|
$this->prepare();
|
|
|
|
$this->grantAccess('addPost');
|
|
$this->grantAccess('addPostTags');
|
|
$this->grantAccess('addPostContent');
|
|
|
|
$this->login($this->mockUser());
|
|
|
|
$post = $this->assert->doesNotThrow(function()
|
|
{
|
|
return Api::run(
|
|
new AddPostJob(),
|
|
[
|
|
JobArgs::ARG_ANONYMOUS => true,
|
|
JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'),
|
|
JobArgs::ARG_NEW_TAG_NAMES => ['kamen', 'raider'],
|
|
]);
|
|
});
|
|
|
|
$this->assert->areEqual(
|
|
file_get_contents($post->getFullPath()),
|
|
file_get_contents($this->getPath('image.jpg')));
|
|
$this->assert->areNotEqual(Auth::getCurrentUser()->getId(), $post->getUploaderId());
|
|
$this->assert->areEqual(null, $post->getUploaderId());
|
|
}
|
|
|
|
public function testPartialPrivilegeFail()
|
|
{
|
|
$this->prepare();
|
|
|
|
$this->grantAccess('addPost');
|
|
$this->grantAccess('addPostSafety');
|
|
$this->grantAccess('addPostTags');
|
|
$this->grantAccess('addPostContent');
|
|
|
|
$this->assert->throws(function()
|
|
{
|
|
Api::run(
|
|
new AddPostJob(),
|
|
[
|
|
JobArgs::ARG_NEW_SAFETY => PostSafety::Safe,
|
|
JobArgs::ARG_NEW_SOURCE => '',
|
|
JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'),
|
|
]);
|
|
}, 'Insufficient privilege');
|
|
}
|
|
|
|
public function testInvalidSafety()
|
|
{
|
|
$this->prepare();
|
|
|
|
$this->grantAccess('addPost');
|
|
$this->grantAccess('addPostTags');
|
|
$this->grantAccess('addPostContent');
|
|
$this->grantAccess('addPostSafety');
|
|
|
|
$this->assert->throws(function()
|
|
{
|
|
Api::run(
|
|
new AddPostJob(),
|
|
[
|
|
JobArgs::ARG_NEW_SAFETY => 666,
|
|
JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'),
|
|
JobArgs::ARG_NEW_TAG_NAMES => ['kamen', 'raider'],
|
|
]);
|
|
}, 'Invalid safety type');
|
|
}
|
|
|
|
public function testNoContentFail()
|
|
{
|
|
$this->prepare();
|
|
|
|
$this->grantAccess('addPost');
|
|
$this->grantAccess('addPostTags');
|
|
$this->grantAccess('addPostContent');
|
|
|
|
$this->assert->throws(function()
|
|
{
|
|
Api::run(
|
|
new AddPostJob(),
|
|
[
|
|
JobArgs::ARG_TAG_NAMES => ['kamen', 'raider'],
|
|
]);
|
|
}, 'No post type detected');
|
|
}
|
|
|
|
public function testEmptyTagsFail()
|
|
{
|
|
$this->prepare();
|
|
|
|
$this->grantAccess('addPost');
|
|
$this->grantAccess('addPostTags');
|
|
$this->grantAccess('addPostContent');
|
|
|
|
$this->assert->throws(function()
|
|
{
|
|
Api::run(
|
|
new AddPostJob(),
|
|
[
|
|
JobArgs::ARG_NEW_POST_CONTENT => new ApiFileInput($this->getPath('image.jpg'), 'test.jpg'),
|
|
]);
|
|
}, 'No tags set');
|
|
}
|
|
|
|
public function testLogBuffering()
|
|
{
|
|
$this->testSaving();
|
|
|
|
$logPath = Logger::getLogPath();
|
|
$x = file_get_contents($logPath);
|
|
$lines = array_filter(explode("\n", $x));
|
|
$this->assert->areEqual(1, count($lines));
|
|
}
|
|
|
|
protected function prepare()
|
|
{
|
|
getConfig()->registration->needEmailForUploading = false;
|
|
}
|
|
}
|