Add ProjecFile and TaskFile models

This commit is contained in:
Frederic Guillot
2016-02-14 15:25:16 -05:00
parent fbb58e08d3
commit 8e25c875f2
30 changed files with 1223 additions and 563 deletions

View File

@@ -1,263 +0,0 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Model\Task;
use Kanboard\Model\File;
use Kanboard\Model\TaskCreation;
use Kanboard\Model\Project;
class FileTest extends Base
{
public function setUp()
{
parent::setUp();
$this->container['objectStorage'] = $this
->getMockBuilder('\Kanboard\Core\ObjectStorage\FileStorage')
->setConstructorArgs(array($this->container))
->setMethods(array('put', 'moveFile', 'remove'))
->getMock();
}
public function testCreation()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $f->create(1, 'test', '/tmp/foo', 10));
$file = $f->getById(1);
$this->assertNotEmpty($file);
$this->assertEquals('test', $file['name']);
$this->assertEquals('/tmp/foo', $file['path']);
$this->assertEquals(0, $file['is_image']);
$this->assertEquals(1, $file['task_id']);
$this->assertEquals(time(), $file['date'], '', 2);
$this->assertEquals(0, $file['user_id']);
$this->assertEquals(10, $file['size']);
$this->assertEquals(2, $f->create(1, 'test2.png', '/tmp/foobar', 10));
$file = $f->getById(2);
$this->assertNotEmpty($file);
$this->assertEquals('test2.png', $file['name']);
$this->assertEquals('/tmp/foobar', $file['path']);
$this->assertEquals(1, $file['is_image']);
}
public function testCreationFileNameTooLong()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertNotFalse($f->create(1, 'test', '/tmp/foo', 10));
$this->assertNotFalse($f->create(1, str_repeat('a', 1000), '/tmp/foo', 10));
$files = $f->getAll(1);
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertEquals(str_repeat('a', 255), $files[0]['name']);
$this->assertEquals('test', $files[1]['name']);
}
public function testIsImage()
{
$f = new File($this->container);
$this->assertTrue($f->isImage('test.png'));
$this->assertTrue($f->isImage('test.jpeg'));
$this->assertTrue($f->isImage('test.gif'));
$this->assertTrue($f->isImage('test.jpg'));
$this->assertTrue($f->isImage('test.JPG'));
$this->assertFalse($f->isImage('test.bmp'));
$this->assertFalse($f->isImage('test'));
$this->assertFalse($f->isImage('test.pdf'));
}
public function testGeneratePath()
{
$f = new File($this->container);
$this->assertStringStartsWith('12'.DIRECTORY_SEPARATOR.'34'.DIRECTORY_SEPARATOR, $f->generatePath(12, 34, 'test.png'));
$this->assertNotEquals($f->generatePath(12, 34, 'test1.png'), $f->generatePath(12, 34, 'test2.png'));
}
public function testUploadScreenshot()
{
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$data = base64_encode('image data');
$f = $this
->getMockBuilder('\Kanboard\Model\File')
->setConstructorArgs(array($this->container))
->setMethods(array('generateThumbnailFromData'))
->getMock();
$this->container['objectStorage']
->expects($this->once())
->method('put')
->with(
$this->stringContains('1'.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR),
$this->equalTo(base64_decode($data))
)
->will($this->returnValue(true));
$f->expects($this->once())
->method('generateThumbnailFromData');
$this->assertEquals(1, $f->uploadScreenshot(1, 1, $data));
$file = $f->getById(1);
$this->assertNotEmpty($file);
$this->assertStringStartsWith('Screenshot taken ', $file['name']);
$this->assertStringStartsWith('1'.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR, $file['path']);
$this->assertEquals(1, $file['is_image']);
$this->assertEquals(1, $file['task_id']);
$this->assertEquals(time(), $file['date'], '', 2);
$this->assertEquals(0, $file['user_id']);
$this->assertEquals(10, $file['size']);
}
public function testUploadFileContent()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$data = base64_encode('file data');
$this->container['objectStorage']
->expects($this->once())
->method('put')
->with(
$this->stringContains('1'.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR),
$this->equalTo(base64_decode($data))
)
->will($this->returnValue(true));
$this->assertEquals(1, $f->uploadContent(1, 1, 'my file.pdf', $data));
$file = $f->getById(1);
$this->assertNotEmpty($file);
$this->assertEquals('my file.pdf', $file['name']);
$this->assertStringStartsWith('1'.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR, $file['path']);
$this->assertEquals(0, $file['is_image']);
$this->assertEquals(1, $file['task_id']);
$this->assertEquals(time(), $file['date'], '', 2);
$this->assertEquals(0, $file['user_id']);
$this->assertEquals(9, $file['size']);
}
public function testGetAll()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $f->create(1, 'B.pdf', '/tmp/foo', 10));
$this->assertEquals(2, $f->create(1, 'A.png', '/tmp/foo', 10));
$this->assertEquals(3, $f->create(1, 'D.doc', '/tmp/foo', 10));
$this->assertEquals(4, $f->create(1, 'C.JPG', '/tmp/foo', 10));
$files = $f->getAll(1);
$this->assertNotEmpty($files);
$this->assertCount(4, $files);
$this->assertEquals('A.png', $files[0]['name']);
$this->assertEquals('B.pdf', $files[1]['name']);
$this->assertEquals('C.JPG', $files[2]['name']);
$this->assertEquals('D.doc', $files[3]['name']);
$files = $f->getAllImages(1);
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertEquals('A.png', $files[0]['name']);
$this->assertEquals('C.JPG', $files[1]['name']);
$files = $f->getAllDocuments(1);
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertEquals('B.pdf', $files[0]['name']);
$this->assertEquals('D.doc', $files[1]['name']);
}
public function testRemove()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $f->create(1, 'B.pdf', DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo1', 10));
$this->assertEquals(2, $f->create(1, 'A.png', DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo2', 10));
$this->assertEquals(3, $f->create(1, 'D.doc', DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo3', 10));
$this->container['objectStorage']
->expects($this->at(0))
->method('remove')
->with(
$this->equalTo(DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo2')
)
->will($this->returnValue(true));
$this->container['objectStorage']
->expects($this->at(1))
->method('remove')
->with(
$this->equalTo('thumbnails'.DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo2')
)
->will($this->returnValue(true));
$this->container['objectStorage']
->expects($this->at(2))
->method('remove')
->with(
$this->equalTo(DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo1')
)
->will($this->returnValue(true));
$this->container['objectStorage']
->expects($this->at(3))
->method('remove')
->with(
$this->equalTo(DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo3')
)
->will($this->returnValue(true));
$this->assertTrue($f->remove(2));
$files = $f->getAll(1);
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertEquals('B.pdf', $files[0]['name']);
$this->assertEquals('D.doc', $files[1]['name']);
$this->assertTrue($f->removeAll(1));
$files = $f->getAll(1);
$this->assertEmpty($files);
}
}

View File

@@ -7,7 +7,7 @@ use Kanboard\Model\TaskCreation;
use Kanboard\Model\Subtask;
use Kanboard\Model\Comment;
use Kanboard\Model\User;
use Kanboard\Model\File;
use Kanboard\Model\TaskFile;
use Kanboard\Model\Task;
use Kanboard\Model\Project;
use Kanboard\Model\Notification;
@@ -23,7 +23,7 @@ class NotificationTest extends Base
$tc = new TaskCreation($this->container);
$s = new Subtask($this->container);
$c = new Comment($this->container);
$f = new File($this->container);
$f = new TaskFile($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));

View File

@@ -9,7 +9,6 @@ use Kanboard\Model\ProjectActivity;
use Kanboard\Model\Project;
use Kanboard\Model\Subtask;
use Kanboard\Model\Comment;
use Kanboard\Model\File;
class ProjectActivityTest extends Base
{

View File

@@ -0,0 +1,311 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Model\ProjectFile;
use Kanboard\Model\Project;
class ProjectFileTest extends Base
{
public function testCreation()
{
$projectModel = new Project($this->container);
$fileModel = new ProjectFile($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'test', '/tmp/foo', 10));
$file = $fileModel->getById(1);
$this->assertEquals('test', $file['name']);
$this->assertEquals('/tmp/foo', $file['path']);
$this->assertEquals(0, $file['is_image']);
$this->assertEquals(1, $file['project_id']);
$this->assertEquals(time(), $file['date'], '', 2);
$this->assertEquals(0, $file['user_id']);
$this->assertEquals(10, $file['size']);
$this->assertEquals(2, $fileModel->create(1, 'test2.png', '/tmp/foobar', 10));
$file = $fileModel->getById(2);
$this->assertEquals('test2.png', $file['name']);
$this->assertEquals('/tmp/foobar', $file['path']);
$this->assertEquals(1, $file['is_image']);
}
public function testCreationWithFileNameTooLong()
{
$projectModel = new Project($this->container);
$fileModel = new ProjectFile($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertNotFalse($fileModel->create(1, 'test', '/tmp/foo', 10));
$this->assertNotFalse($fileModel->create(1, str_repeat('a', 1000), '/tmp/foo', 10));
$files = $fileModel->getAll(1);
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertEquals(str_repeat('a', 255), $files[0]['name']);
$this->assertEquals('test', $files[1]['name']);
}
public function testCreationWithSessionOpen()
{
$this->container['sessionStorage']->user = array('id' => 1);
$projectModel = new Project($this->container);
$fileModel = new ProjectFile($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'test', '/tmp/foo', 10));
$file = $fileModel->getById(1);
$this->assertEquals('test', $file['name']);
$this->assertEquals(1, $file['user_id']);
}
public function testGetAll()
{
$projectModel = new Project($this->container);
$fileModel = new ProjectFile($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'B.pdf', '/tmp/foo', 10));
$this->assertEquals(2, $fileModel->create(1, 'A.png', '/tmp/foo', 10));
$this->assertEquals(3, $fileModel->create(1, 'D.doc', '/tmp/foo', 10));
$this->assertEquals(4, $fileModel->create(1, 'C.JPG', '/tmp/foo', 10));
$fileModeliles = $fileModel->getAll(1);
$this->assertNotEmpty($fileModeliles);
$this->assertCount(4, $fileModeliles);
$this->assertEquals('A.png', $fileModeliles[0]['name']);
$this->assertEquals('B.pdf', $fileModeliles[1]['name']);
$this->assertEquals('C.JPG', $fileModeliles[2]['name']);
$this->assertEquals('D.doc', $fileModeliles[3]['name']);
$fileModeliles = $fileModel->getAllImages(1);
$this->assertNotEmpty($fileModeliles);
$this->assertCount(2, $fileModeliles);
$this->assertEquals('A.png', $fileModeliles[0]['name']);
$this->assertEquals('C.JPG', $fileModeliles[1]['name']);
$fileModeliles = $fileModel->getAllDocuments(1);
$this->assertNotEmpty($fileModeliles);
$this->assertCount(2, $fileModeliles);
$this->assertEquals('B.pdf', $fileModeliles[0]['name']);
$this->assertEquals('D.doc', $fileModeliles[1]['name']);
}
public function testGetThumbnailPath()
{
$fileModel = new ProjectFile($this->container);
$this->assertEquals('thumbnails'.DIRECTORY_SEPARATOR.'test', $fileModel->getThumbnailPath('test'));
}
public function testGeneratePath()
{
$fileModel = new ProjectFile($this->container);
$this->assertStringStartsWith('projects'.DIRECTORY_SEPARATOR.'34'.DIRECTORY_SEPARATOR, $fileModel->generatePath(34, 'test.png'));
$this->assertNotEquals($fileModel->generatePath(34, 'test1.png'), $fileModel->generatePath(34, 'test2.png'));
}
public function testUploadFiles()
{
$fileModel = $this
->getMockBuilder('\Kanboard\Model\ProjectFile')
->setConstructorArgs(array($this->container))
->setMethods(array('generateThumbnailFromFile'))
->getMock();
$projectModel = new Project($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$files = array(
'name' => array(
'file1.png',
'file2.doc',
),
'tmp_name' => array(
'/tmp/phpYzdqkD',
'/tmp/phpeEwEWG',
),
'error' => array(
UPLOAD_ERR_OK,
UPLOAD_ERR_OK,
),
'size' => array(
123,
456,
),
);
$fileModel
->expects($this->once())
->method('generateThumbnailFromFile');
$this->container['objectStorage']
->expects($this->at(0))
->method('moveUploadedFile')
->with($this->equalTo('/tmp/phpYzdqkD'), $this->anything());
$this->container['objectStorage']
->expects($this->at(1))
->method('moveUploadedFile')
->with($this->equalTo('/tmp/phpeEwEWG'), $this->anything());
$this->assertTrue($fileModel->uploadFiles(1, $files));
$files = $fileModel->getAll(1);
$this->assertCount(2, $files);
$this->assertEquals(1, $files[0]['id']);
$this->assertEquals('file1.png', $files[0]['name']);
$this->assertEquals(1, $files[0]['is_image']);
$this->assertEquals(1, $files[0]['project_id']);
$this->assertEquals(0, $files[0]['user_id']);
$this->assertEquals(123, $files[0]['size']);
$this->assertEquals(time(), $files[0]['date'], '', 2);
$this->assertEquals(2, $files[1]['id']);
$this->assertEquals('file2.doc', $files[1]['name']);
$this->assertEquals(0, $files[1]['is_image']);
$this->assertEquals(1, $files[1]['project_id']);
$this->assertEquals(0, $files[1]['user_id']);
$this->assertEquals(456, $files[1]['size']);
$this->assertEquals(time(), $files[1]['date'], '', 2);
}
public function testUploadFilesWithEmptyFiles()
{
$fileModel = new ProjectFile($this->container);
$this->assertFalse($fileModel->uploadFiles(1, array()));
}
public function testUploadFilesWithUploadError()
{
$files = array(
'name' => array(
'file1.png',
'file2.doc',
),
'tmp_name' => array(
'',
'/tmp/phpeEwEWG',
),
'error' => array(
UPLOAD_ERR_CANT_WRITE,
UPLOAD_ERR_OK,
),
'size' => array(
123,
456,
),
);
$fileModel = new ProjectFile($this->container);
$this->assertFalse($fileModel->uploadFiles(1, $files));
}
public function testUploadFilesWithObjectStorageError()
{
$files = array(
'name' => array(
'file1.csv',
'file2.doc',
),
'tmp_name' => array(
'/tmp/phpYzdqkD',
'/tmp/phpeEwEWG',
),
'error' => array(
UPLOAD_ERR_OK,
UPLOAD_ERR_OK,
),
'size' => array(
123,
456,
),
);
$this->container['objectStorage']
->expects($this->at(0))
->method('moveUploadedFile')
->with($this->equalTo('/tmp/phpYzdqkD'), $this->anything())
->will($this->throwException(new \Kanboard\Core\ObjectStorage\ObjectStorageException('test')));
$fileModel = new ProjectFile($this->container);
$this->assertFalse($fileModel->uploadFiles(1, $files));
}
public function testUploadFileContent()
{
$fileModel = $this
->getMockBuilder('\Kanboard\Model\ProjectFile')
->setConstructorArgs(array($this->container))
->setMethods(array('generateThumbnailFromFile'))
->getMock();
$projectModel = new Project($this->container);
$data = 'test';
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->container['objectStorage']
->expects($this->once())
->method('put')
->with($this->anything(), $this->equalTo($data));
$this->assertEquals(1, $fileModel->uploadContent(1, 'test.doc', base64_encode($data)));
$files = $fileModel->getAll(1);
$this->assertCount(1, $files);
$this->assertEquals(1, $files[0]['id']);
$this->assertEquals('test.doc', $files[0]['name']);
$this->assertEquals(0, $files[0]['is_image']);
$this->assertEquals(1, $files[0]['project_id']);
$this->assertEquals(0, $files[0]['user_id']);
$this->assertEquals(4, $files[0]['size']);
$this->assertEquals(time(), $files[0]['date'], '', 2);
}
public function testUploadImageContent()
{
$fileModel = $this
->getMockBuilder('\Kanboard\Model\ProjectFile')
->setConstructorArgs(array($this->container))
->setMethods(array('generateThumbnailFromFile'))
->getMock();
$projectModel = new Project($this->container);
$data = 'test';
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$fileModel
->expects($this->once())
->method('generateThumbnailFromFile');
$this->container['objectStorage']
->expects($this->once())
->method('put')
->with($this->anything(), $this->equalTo($data));
$this->assertEquals(1, $fileModel->uploadContent(1, 'test.png', base64_encode($data)));
$files = $fileModel->getAll(1);
$this->assertCount(1, $files);
$this->assertEquals(1, $files[0]['id']);
$this->assertEquals('test.png', $files[0]['name']);
$this->assertEquals(1, $files[0]['is_image']);
$this->assertEquals(1, $files[0]['project_id']);
$this->assertEquals(0, $files[0]['user_id']);
$this->assertEquals(4, $files[0]['size']);
$this->assertEquals(time(), $files[0]['date'], '', 2);
}
}

View File

@@ -0,0 +1,458 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Model\Task;
use Kanboard\Model\TaskFile;
use Kanboard\Model\TaskCreation;
use Kanboard\Model\Project;
class TaskFileTest extends Base
{
public function testCreation()
{
$projectModel = new Project($this->container);
$fileModel = new TaskFile($this->container);
$taskCreationModel = new TaskCreation($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'test', '/tmp/foo', 10));
$file = $fileModel->getById(1);
$this->assertEquals('test', $file['name']);
$this->assertEquals('/tmp/foo', $file['path']);
$this->assertEquals(0, $file['is_image']);
$this->assertEquals(1, $file['task_id']);
$this->assertEquals(time(), $file['date'], '', 2);
$this->assertEquals(0, $file['user_id']);
$this->assertEquals(10, $file['size']);
$this->assertEquals(2, $fileModel->create(1, 'test2.png', '/tmp/foobar', 10));
$file = $fileModel->getById(2);
$this->assertEquals('test2.png', $file['name']);
$this->assertEquals('/tmp/foobar', $file['path']);
$this->assertEquals(1, $file['is_image']);
}
public function testCreationWithFileNameTooLong()
{
$projectModel = new Project($this->container);
$fileModel = new TaskFile($this->container);
$taskCreationModel = new TaskCreation($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->assertNotFalse($fileModel->create(1, 'test', '/tmp/foo', 10));
$this->assertNotFalse($fileModel->create(1, str_repeat('a', 1000), '/tmp/foo', 10));
$files = $fileModel->getAll(1);
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertEquals(str_repeat('a', 255), $files[0]['name']);
$this->assertEquals('test', $files[1]['name']);
}
public function testCreationWithSessionOpen()
{
$this->container['sessionStorage']->user = array('id' => 1);
$projectModel = new Project($this->container);
$fileModel = new TaskFile($this->container);
$taskCreationModel = new TaskCreation($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'test', '/tmp/foo', 10));
$file = $fileModel->getById(1);
$this->assertEquals('test', $file['name']);
$this->assertEquals(1, $file['user_id']);
}
public function testGetAll()
{
$projectModel = new Project($this->container);
$fileModel = new TaskFile($this->container);
$taskCreationModel = new TaskCreation($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'B.pdf', '/tmp/foo', 10));
$this->assertEquals(2, $fileModel->create(1, 'A.png', '/tmp/foo', 10));
$this->assertEquals(3, $fileModel->create(1, 'D.doc', '/tmp/foo', 10));
$this->assertEquals(4, $fileModel->create(1, 'C.JPG', '/tmp/foo', 10));
$fileModeliles = $fileModel->getAll(1);
$this->assertNotEmpty($fileModeliles);
$this->assertCount(4, $fileModeliles);
$this->assertEquals('A.png', $fileModeliles[0]['name']);
$this->assertEquals('B.pdf', $fileModeliles[1]['name']);
$this->assertEquals('C.JPG', $fileModeliles[2]['name']);
$this->assertEquals('D.doc', $fileModeliles[3]['name']);
$fileModeliles = $fileModel->getAllImages(1);
$this->assertNotEmpty($fileModeliles);
$this->assertCount(2, $fileModeliles);
$this->assertEquals('A.png', $fileModeliles[0]['name']);
$this->assertEquals('C.JPG', $fileModeliles[1]['name']);
$fileModeliles = $fileModel->getAllDocuments(1);
$this->assertNotEmpty($fileModeliles);
$this->assertCount(2, $fileModeliles);
$this->assertEquals('B.pdf', $fileModeliles[0]['name']);
$this->assertEquals('D.doc', $fileModeliles[1]['name']);
}
public function testIsImage()
{
$fileModel = new TaskFile($this->container);
$this->assertTrue($fileModel->isImage('test.png'));
$this->assertTrue($fileModel->isImage('test.jpeg'));
$this->assertTrue($fileModel->isImage('test.gif'));
$this->assertTrue($fileModel->isImage('test.jpg'));
$this->assertTrue($fileModel->isImage('test.JPG'));
$this->assertFalse($fileModel->isImage('test.bmp'));
$this->assertFalse($fileModel->isImage('test'));
$this->assertFalse($fileModel->isImage('test.pdf'));
}
public function testGetMimeType()
{
$fileModel = new TaskFile($this->container);
$this->assertEquals('image/jpeg', $fileModel->getImageMimeType('My File.JPG'));
$this->assertEquals('image/jpeg', $fileModel->getImageMimeType('My File.jpeg'));
$this->assertEquals('image/png', $fileModel->getImageMimeType('My File.PNG'));
$this->assertEquals('image/gif', $fileModel->getImageMimeType('My File.gif'));
$this->assertEquals('image/jpeg', $fileModel->getImageMimeType('My File.bmp'));
$this->assertEquals('image/jpeg', $fileModel->getImageMimeType('My File'));
}
public function testGetThumbnailPath()
{
$fileModel = new TaskFile($this->container);
$this->assertEquals('thumbnails'.DIRECTORY_SEPARATOR.'test', $fileModel->getThumbnailPath('test'));
}
public function testGeneratePath()
{
$fileModel = new TaskFile($this->container);
$this->assertStringStartsWith('tasks'.DIRECTORY_SEPARATOR.'34'.DIRECTORY_SEPARATOR, $fileModel->generatePath(34, 'test.png'));
$this->assertNotEquals($fileModel->generatePath(34, 'test1.png'), $fileModel->generatePath(34, 'test2.png'));
}
public function testUploadFiles()
{
$fileModel = $this
->getMockBuilder('\Kanboard\Model\TaskFile')
->setConstructorArgs(array($this->container))
->setMethods(array('generateThumbnailFromFile'))
->getMock();
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$files = array(
'name' => array(
'file1.png',
'file2.doc',
),
'tmp_name' => array(
'/tmp/phpYzdqkD',
'/tmp/phpeEwEWG',
),
'error' => array(
UPLOAD_ERR_OK,
UPLOAD_ERR_OK,
),
'size' => array(
123,
456,
),
);
$fileModel
->expects($this->once())
->method('generateThumbnailFromFile');
$this->container['objectStorage']
->expects($this->at(0))
->method('moveUploadedFile')
->with($this->equalTo('/tmp/phpYzdqkD'), $this->anything());
$this->container['objectStorage']
->expects($this->at(1))
->method('moveUploadedFile')
->with($this->equalTo('/tmp/phpeEwEWG'), $this->anything());
$this->assertTrue($fileModel->uploadFiles(1, $files));
$files = $fileModel->getAll(1);
$this->assertCount(2, $files);
$this->assertEquals(1, $files[0]['id']);
$this->assertEquals('file1.png', $files[0]['name']);
$this->assertEquals(1, $files[0]['is_image']);
$this->assertEquals(1, $files[0]['task_id']);
$this->assertEquals(0, $files[0]['user_id']);
$this->assertEquals(123, $files[0]['size']);
$this->assertEquals(time(), $files[0]['date'], '', 2);
$this->assertEquals(2, $files[1]['id']);
$this->assertEquals('file2.doc', $files[1]['name']);
$this->assertEquals(0, $files[1]['is_image']);
$this->assertEquals(1, $files[1]['task_id']);
$this->assertEquals(0, $files[1]['user_id']);
$this->assertEquals(456, $files[1]['size']);
$this->assertEquals(time(), $files[1]['date'], '', 2);
}
public function testUploadFilesWithEmptyFiles()
{
$fileModel = new TaskFile($this->container);
$this->assertFalse($fileModel->uploadFiles(1, array()));
}
public function testUploadFilesWithUploadError()
{
$files = array(
'name' => array(
'file1.png',
'file2.doc',
),
'tmp_name' => array(
'',
'/tmp/phpeEwEWG',
),
'error' => array(
UPLOAD_ERR_CANT_WRITE,
UPLOAD_ERR_OK,
),
'size' => array(
123,
456,
),
);
$fileModel = new TaskFile($this->container);
$this->assertFalse($fileModel->uploadFiles(1, $files));
}
public function testUploadFilesWithObjectStorageError()
{
$files = array(
'name' => array(
'file1.csv',
'file2.doc',
),
'tmp_name' => array(
'/tmp/phpYzdqkD',
'/tmp/phpeEwEWG',
),
'error' => array(
UPLOAD_ERR_OK,
UPLOAD_ERR_OK,
),
'size' => array(
123,
456,
),
);
$this->container['objectStorage']
->expects($this->at(0))
->method('moveUploadedFile')
->with($this->equalTo('/tmp/phpYzdqkD'), $this->anything())
->will($this->throwException(new \Kanboard\Core\ObjectStorage\ObjectStorageException('test')));
$fileModel = new TaskFile($this->container);
$this->assertFalse($fileModel->uploadFiles(1, $files));
}
public function testUploadFileContent()
{
$fileModel = $this
->getMockBuilder('\Kanboard\Model\TaskFile')
->setConstructorArgs(array($this->container))
->setMethods(array('generateThumbnailFromFile'))
->getMock();
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$data = 'test';
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->container['objectStorage']
->expects($this->once())
->method('put')
->with($this->anything(), $this->equalTo($data));
$this->assertEquals(1, $fileModel->uploadContent(1, 'test.doc', base64_encode($data)));
$files = $fileModel->getAll(1);
$this->assertCount(1, $files);
$this->assertEquals(1, $files[0]['id']);
$this->assertEquals('test.doc', $files[0]['name']);
$this->assertEquals(0, $files[0]['is_image']);
$this->assertEquals(1, $files[0]['task_id']);
$this->assertEquals(0, $files[0]['user_id']);
$this->assertEquals(4, $files[0]['size']);
$this->assertEquals(time(), $files[0]['date'], '', 2);
}
public function testUploadFileContentWithObjectStorageError()
{
$fileModel = $this
->getMockBuilder('\Kanboard\Model\TaskFile')
->setConstructorArgs(array($this->container))
->setMethods(array('generateThumbnailFromFile'))
->getMock();
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$data = 'test';
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->container['objectStorage']
->expects($this->once())
->method('put')
->with($this->anything(), $this->equalTo($data))
->will($this->throwException(new \Kanboard\Core\ObjectStorage\ObjectStorageException('test')));
$this->assertFalse($fileModel->uploadContent(1, 'test.doc', base64_encode($data)));
}
public function testUploadScreenshot()
{
$fileModel = $this
->getMockBuilder('\Kanboard\Model\TaskFile')
->setConstructorArgs(array($this->container))
->setMethods(array('generateThumbnailFromFile'))
->getMock();
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$data = 'test';
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$fileModel
->expects($this->once())
->method('generateThumbnailFromFile');
$this->container['objectStorage']
->expects($this->once())
->method('put')
->with($this->anything(), $this->equalTo($data));
$this->assertEquals(1, $fileModel->uploadScreenshot(1, base64_encode($data)));
$files = $fileModel->getAll(1);
$this->assertCount(1, $files);
$this->assertEquals(1, $files[0]['id']);
$this->assertStringStartsWith('Screenshot taken ', $files[0]['name']);
$this->assertEquals(1, $files[0]['is_image']);
$this->assertEquals(1, $files[0]['task_id']);
$this->assertEquals(0, $files[0]['user_id']);
$this->assertEquals(4, $files[0]['size']);
$this->assertEquals(time(), $files[0]['date'], '', 2);
}
public function testRemove()
{
$fileModel = new TaskFile($this->container);
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'test', 'tmp/foo', 10));
$this->container['objectStorage']
->expects($this->once())
->method('remove')
->with('tmp/foo');
$this->assertTrue($fileModel->remove(1));
}
public function testRemoveWithObjectStorageError()
{
$fileModel = new TaskFile($this->container);
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'test', 'tmp/foo', 10));
$this->container['objectStorage']
->expects($this->once())
->method('remove')
->with('tmp/foo')
->will($this->throwException(new \Kanboard\Core\ObjectStorage\ObjectStorageException('test')));
$this->assertFalse($fileModel->remove(1));
}
public function testRemoveImage()
{
$fileModel = new TaskFile($this->container);
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'image.gif', 'tmp/image.gif', 10));
$this->container['objectStorage']
->expects($this->at(0))
->method('remove')
->with('tmp/image.gif');
$this->container['objectStorage']
->expects($this->at(1))
->method('remove')
->with('thumbnails'.DIRECTORY_SEPARATOR.'tmp/image.gif');
$this->assertTrue($fileModel->remove(1));
}
public function testRemoveAll()
{
$fileModel = new TaskFile($this->container);
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test')));
$this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $fileModel->create(1, 'test', 'tmp/foo', 10));
$this->assertEquals(2, $fileModel->create(1, 'test', 'tmp/foo', 10));
$this->container['objectStorage']
->expects($this->exactly(2))
->method('remove')
->with('tmp/foo');
$this->assertTrue($fileModel->removeAll(1));
}
}

View File

@@ -9,7 +9,6 @@ use Kanboard\Model\Comment;
use Kanboard\Model\User;
use Kanboard\Model\Group;
use Kanboard\Model\GroupMember;
use Kanboard\Model\File;
use Kanboard\Model\Project;
use Kanboard\Model\ProjectPermission;
use Kanboard\Model\Task;

View File

@@ -7,7 +7,6 @@ use Kanboard\Model\TaskCreation;
use Kanboard\Model\Subtask;
use Kanboard\Model\Comment;
use Kanboard\Model\User;
use Kanboard\Model\File;
use Kanboard\Model\Task;
use Kanboard\Model\Project;
use Kanboard\Model\UserUnreadNotification;