From 8e25c875f26b05e3138fb4d09d9f720456c09f76 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 14 Feb 2016 15:25:16 -0500 Subject: Add ProjecFile and TaskFile models --- .../AverageTimeSpentColumnAnalyticTest.php | 96 ++--- tests/units/Base.php | 6 + tests/units/Model/FileTest.php | 263 ------------ tests/units/Model/NotificationTest.php | 4 +- tests/units/Model/ProjectActivityTest.php | 1 - tests/units/Model/ProjectFileTest.php | 311 ++++++++++++++ tests/units/Model/TaskFileTest.php | 458 +++++++++++++++++++++ tests/units/Model/UserNotificationTest.php | 1 - tests/units/Model/UserUnreadNotificationTest.php | 1 - tests/units/Notification/MailTest.php | 4 +- 10 files changed, 819 insertions(+), 326 deletions(-) delete mode 100644 tests/units/Model/FileTest.php create mode 100644 tests/units/Model/ProjectFileTest.php create mode 100644 tests/units/Model/TaskFileTest.php (limited to 'tests') diff --git a/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php b/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php index 8ad6d1e7..4e01bfa9 100644 --- a/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php +++ b/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php @@ -28,34 +28,26 @@ class AverageTimeSpentColumnAnalyticTest extends Base $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800)); $stats = $averageLeadCycleTimeAnalytic->build(1); - $expected = array( - 1 => array( - 'count' => 2, - 'time_spent' => 3600+1800, - 'average' => (int) ((3600+1800)/2), - 'title' => 'Backlog', - ), - 2 => array( - 'count' => 0, - 'time_spent' => 0, - 'average' => 0, - 'title' => 'Ready', - ), - 3 => array( - 'count' => 0, - 'time_spent' => 0, - 'average' => 0, - 'title' => 'Work in progress', - ), - 4 => array( - 'count' => 0, - 'time_spent' => 0, - 'average' => 0, - 'title' => 'Done', - ) - ); - - $this->assertEquals($expected, $stats); + + $this->assertEquals(2, $stats[1]['count']); + $this->assertEquals(3600+1800, $stats[1]['time_spent'], '', 3); + $this->assertEquals((int) ((3600+1800)/2), $stats[1]['average'], '', 3); + $this->assertEquals('Backlog', $stats[1]['title']); + + $this->assertEquals(0, $stats[2]['count']); + $this->assertEquals(0, $stats[2]['time_spent'], '', 3); + $this->assertEquals(0, $stats[2]['average'], '', 3); + $this->assertEquals('Ready', $stats[2]['title']); + + $this->assertEquals(0, $stats[3]['count']); + $this->assertEquals(0, $stats[3]['time_spent'], '', 3); + $this->assertEquals(0, $stats[3]['average'], '', 3); + $this->assertEquals('Work in progress', $stats[3]['title']); + + $this->assertEquals(0, $stats[4]['count']); + $this->assertEquals(0, $stats[4]['time_spent'], '', 3); + $this->assertEquals(0, $stats[4]['average'], '', 3); + $this->assertEquals('Done', $stats[4]['title']); } public function testAverageWithTransitions() @@ -85,33 +77,25 @@ class AverageTimeSpentColumnAnalyticTest extends Base } $stats = $averageLeadCycleTimeAnalytic->build(1); - $expected = array( - 1 => array( - 'count' => 2, - 'time_spent' => 3600+1800, - 'average' => (int) ((3600+1800)/2), - 'title' => 'Backlog', - ), - 2 => array( - 'count' => 0, - 'time_spent' => 0, - 'average' => 0, - 'title' => 'Ready', - ), - 3 => array( - 'count' => 2, - 'time_spent' => 1800, - 'average' => 900, - 'title' => 'Work in progress', - ), - 4 => array( - 'count' => 0, - 'time_spent' => 0, - 'average' => 0, - 'title' => 'Done', - ) - ); - - $this->assertEquals($expected, $stats); + + $this->assertEquals(2, $stats[1]['count']); + $this->assertEquals(3600+1800, $stats[1]['time_spent'], '', 3); + $this->assertEquals((int) ((3600+1800)/2), $stats[1]['average'], '', 3); + $this->assertEquals('Backlog', $stats[1]['title']); + + $this->assertEquals(0, $stats[2]['count']); + $this->assertEquals(0, $stats[2]['time_spent'], '', 3); + $this->assertEquals(0, $stats[2]['average'], '', 3); + $this->assertEquals('Ready', $stats[2]['title']); + + $this->assertEquals(2, $stats[3]['count']); + $this->assertEquals(1800, $stats[3]['time_spent'], '', 3); + $this->assertEquals(900, $stats[3]['average'], '', 3); + $this->assertEquals('Work in progress', $stats[3]['title']); + + $this->assertEquals(0, $stats[4]['count']); + $this->assertEquals(0, $stats[4]['time_spent'], '', 3); + $this->assertEquals(0, $stats[4]['average'], '', 3); + $this->assertEquals('Done', $stats[4]['title']); } } diff --git a/tests/units/Base.php b/tests/units/Base.php index 22c6304f..eb9fc68b 100644 --- a/tests/units/Base.php +++ b/tests/units/Base.php @@ -67,6 +67,12 @@ abstract class Base extends PHPUnit_Framework_TestCase ->setMethods(array('getType', 'getSelectedTypes')) ->getMock(); + $this->container['objectStorage'] = $this + ->getMockBuilder('\Kanboard\Core\ObjectStorage\FileStorage') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('put', 'moveFile', 'remove', 'moveUploadedFile')) + ->getMock(); + $this->container['sessionStorage'] = new SessionStorage; $this->container->register(new ActionProvider); diff --git a/tests/units/Model/FileTest.php b/tests/units/Model/FileTest.php deleted file mode 100644 index 29f6ee93..00000000 --- a/tests/units/Model/FileTest.php +++ /dev/null @@ -1,263 +0,0 @@ -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); - } -} diff --git a/tests/units/Model/NotificationTest.php b/tests/units/Model/NotificationTest.php index 7f9977ce..03ee5867 100644 --- a/tests/units/Model/NotificationTest.php +++ b/tests/units/Model/NotificationTest.php @@ -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))); diff --git a/tests/units/Model/ProjectActivityTest.php b/tests/units/Model/ProjectActivityTest.php index 10201aa8..04d3004d 100644 --- a/tests/units/Model/ProjectActivityTest.php +++ b/tests/units/Model/ProjectActivityTest.php @@ -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 { diff --git a/tests/units/Model/ProjectFileTest.php b/tests/units/Model/ProjectFileTest.php new file mode 100644 index 00000000..d9b37fbe --- /dev/null +++ b/tests/units/Model/ProjectFileTest.php @@ -0,0 +1,311 @@ +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); + } +} diff --git a/tests/units/Model/TaskFileTest.php b/tests/units/Model/TaskFileTest.php new file mode 100644 index 00000000..753a1fb6 --- /dev/null +++ b/tests/units/Model/TaskFileTest.php @@ -0,0 +1,458 @@ +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)); + } +} diff --git a/tests/units/Model/UserNotificationTest.php b/tests/units/Model/UserNotificationTest.php index 8168a375..e1928661 100644 --- a/tests/units/Model/UserNotificationTest.php +++ b/tests/units/Model/UserNotificationTest.php @@ -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; diff --git a/tests/units/Model/UserUnreadNotificationTest.php b/tests/units/Model/UserUnreadNotificationTest.php index bf274d95..62889bf0 100644 --- a/tests/units/Model/UserUnreadNotificationTest.php +++ b/tests/units/Model/UserUnreadNotificationTest.php @@ -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; diff --git a/tests/units/Notification/MailTest.php b/tests/units/Notification/MailTest.php index 8f343ba3..7dc6aaef 100644 --- a/tests/units/Notification/MailTest.php +++ b/tests/units/Notification/MailTest.php @@ -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\Project; use Kanboard\Model\Task; use Kanboard\Notification\Mail; @@ -23,7 +23,7 @@ class MailTest 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))); -- cgit v1.2.3