diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/integration/ApiTest.php | 14 | ||||
-rw-r--r-- | tests/units/Action/TaskCloseNoActivityTest.php | 43 | ||||
-rw-r--r-- | tests/units/Action/TaskEmailNoActivityTest.php | 103 | ||||
-rw-r--r-- | tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php | 5 | ||||
-rw-r--r-- | tests/units/Auth/GithubAuthTest.php | 89 | ||||
-rw-r--r-- | tests/units/Auth/GitlabAuthTest.php | 89 | ||||
-rw-r--r-- | tests/units/Auth/GoogleAuthTest.php | 89 | ||||
-rw-r--r-- | tests/units/Base.php | 10 | ||||
-rw-r--r-- | tests/units/Core/ExternalLink/ExternalLinkManagerTest.php | 120 | ||||
-rw-r--r-- | tests/units/ExternalLink/AttachmentLinkProviderTest.php | 64 | ||||
-rw-r--r-- | tests/units/ExternalLink/AttachmentLinkTest.php | 18 | ||||
-rw-r--r-- | tests/units/ExternalLink/WebLinkProviderTest.php | 52 | ||||
-rw-r--r-- | tests/units/ExternalLink/WebLinkTest.php | 57 | ||||
-rw-r--r-- | tests/units/Helper/UserHelperTest.php | 12 | ||||
-rw-r--r-- | tests/units/Model/ProjectDuplicationTest.php | 383 | ||||
-rw-r--r-- | tests/units/Model/SubtaskTest.php | 12 | ||||
-rw-r--r-- | tests/units/Model/TaskExternalLinkTest.php | 123 | ||||
-rw-r--r-- | tests/units/Validator/ExternalLinkValidatorTest.php | 63 |
18 files changed, 938 insertions, 408 deletions
diff --git a/tests/integration/ApiTest.php b/tests/integration/ApiTest.php index 798bde42..8b970a6c 100644 --- a/tests/integration/ApiTest.php +++ b/tests/integration/ApiTest.php @@ -563,6 +563,20 @@ class Api extends PHPUnit_Framework_TestCase $this->assertNull($this->client->getUser(2222)); } + public function testGetUserByName() + { + $user = $this->client->getUserByName('toto'); + $this->assertNotFalse($user); + $this->assertTrue(is_array($user)); + $this->assertEquals(2, $user['id']); + + $user = $this->client->getUserByName('manager'); + $this->assertNotEmpty($user); + $this->assertEquals('app-manager', $user['role']); + + $this->assertNull($this->client->getUserByName('nonexistantusername')); + } + public function testUpdateUser() { $user = array(); diff --git a/tests/units/Action/TaskCloseNoActivityTest.php b/tests/units/Action/TaskCloseNoActivityTest.php new file mode 100644 index 00000000..b6e04c47 --- /dev/null +++ b/tests/units/Action/TaskCloseNoActivityTest.php @@ -0,0 +1,43 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Event\TaskListEvent; +use Kanboard\Model\TaskCreation; +use Kanboard\Model\TaskFinder; +use Kanboard\Model\Project; +use Kanboard\Model\Task; +use Kanboard\Action\TaskCloseNoActivity; + +class TaskCloseNoActivityTest extends Base +{ + public function testClose() + { + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskFinderModel = new TaskFinder($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_modification' => strtotime('-10days'))); + + $tasks = $taskFinderModel->getAll(1); + $event = new TaskListEvent(array('tasks' => $tasks, 'project_id' => 1)); + + $action = new TaskCloseNoActivity($this->container); + $action->setProjectId(1); + $action->setParam('duration', 2); + + $this->assertTrue($action->execute($event, Task::EVENT_DAILY_CRONJOB)); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['is_active']); + + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['is_active']); + } +} diff --git a/tests/units/Action/TaskEmailNoActivityTest.php b/tests/units/Action/TaskEmailNoActivityTest.php new file mode 100644 index 00000000..af4baed5 --- /dev/null +++ b/tests/units/Action/TaskEmailNoActivityTest.php @@ -0,0 +1,103 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Event\TaskListEvent; +use Kanboard\Model\TaskCreation; +use Kanboard\Model\TaskFinder; +use Kanboard\Model\Project; +use Kanboard\Model\Task; +use Kanboard\Model\User; +use Kanboard\Action\TaskEmailNoActivity; + +class TaskEmailNoActivityTest extends Base +{ + public function testSendEmail() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskFinderModel = new TaskFinder($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'test', 'email' => 'chuck@norris', 'name' => 'Chuck Norris'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_modification' => strtotime('-10days'))); + + $tasks = $taskFinderModel->getAll(1); + $event = new TaskListEvent(array('tasks' => $tasks, 'project_id' => 1)); + + $action = new TaskEmailNoActivity($this->container); + $action->setProjectId(1); + $action->setParam('user_id', 2); + $action->setParam('subject', 'Old tasks'); + $action->setParam('duration', 2); + + $this->container['emailClient'] + ->expects($this->once()) + ->method('send') + ->with('chuck@norris', 'Chuck Norris', 'Old tasks', $this->anything()); + + $this->assertTrue($action->execute($event, Task::EVENT_DAILY_CRONJOB)); + } + + public function testUserWithNoEmail() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskFinderModel = new TaskFinder($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'test', 'name' => 'Chuck Norris'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_modification' => strtotime('-10days'))); + + $tasks = $taskFinderModel->getAll(1); + $event = new TaskListEvent(array('tasks' => $tasks, 'project_id' => 1)); + + $action = new TaskEmailNoActivity($this->container); + $action->setProjectId(1); + $action->setParam('user_id', 2); + $action->setParam('subject', 'Old tasks'); + $action->setParam('duration', 2); + + $this->container['emailClient'] + ->expects($this->never()) + ->method('send'); + + $this->assertFalse($action->execute($event, Task::EVENT_DAILY_CRONJOB)); + } + + public function testTooRecent() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskFinderModel = new TaskFinder($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'test', 'email' => 'chuck@norris', 'name' => 'Chuck Norris'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $tasks = $taskFinderModel->getAll(1); + $event = new TaskListEvent(array('tasks' => $tasks, 'project_id' => 1)); + + $action = new TaskEmailNoActivity($this->container); + $action->setProjectId(1); + $action->setParam('user_id', 2); + $action->setParam('subject', 'Old tasks'); + $action->setParam('duration', 2); + + $this->container['emailClient'] + ->expects($this->never()) + ->method('send'); + + $this->assertFalse($action->execute($event, Task::EVENT_DAILY_CRONJOB)); + } +} diff --git a/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php b/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php index 75cb181d..8ad6d1e7 100644 --- a/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php +++ b/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php @@ -16,13 +16,14 @@ class AverageTimeSpentColumnAnalyticTest extends Base $taskCreationModel = new TaskCreation($this->container); $projectModel = new Project($this->container); $averageLeadCycleTimeAnalytic = new AverageTimeSpentColumnAnalytic($this->container); - $now = time(); $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $now = time(); + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600)); $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800)); @@ -64,13 +65,13 @@ class AverageTimeSpentColumnAnalyticTest extends Base $taskCreationModel = new TaskCreation($this->container); $projectModel = new Project($this->container); $averageLeadCycleTimeAnalytic = new AverageTimeSpentColumnAnalytic($this->container); - $now = time(); $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $now = time(); $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600)); $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800)); diff --git a/tests/units/Auth/GithubAuthTest.php b/tests/units/Auth/GithubAuthTest.php deleted file mode 100644 index e9ab066f..00000000 --- a/tests/units/Auth/GithubAuthTest.php +++ /dev/null @@ -1,89 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Auth\GithubAuth; -use Kanboard\Model\User; - -class GithubAuthTest extends Base -{ - public function testGetName() - { - $provider = new GithubAuth($this->container); - $this->assertEquals('Github', $provider->getName()); - } - - public function testAuthenticationSuccessful() - { - $profile = array( - 'id' => 1234, - 'email' => 'test@localhost', - 'name' => 'Test', - ); - - $provider = $this - ->getMockBuilder('\Kanboard\Auth\GithubAuth') - ->setConstructorArgs(array($this->container)) - ->setMethods(array( - 'getProfile', - )) - ->getMock(); - - $provider->expects($this->once()) - ->method('getProfile') - ->will($this->returnValue($profile)); - - $this->assertInstanceOf('Kanboard\Auth\GithubAuth', $provider->setCode('1234')); - - $this->assertTrue($provider->authenticate()); - - $user = $provider->getUser(); - $this->assertInstanceOf('Kanboard\User\GithubUserProvider', $user); - $this->assertEquals('Test', $user->getName()); - $this->assertEquals('', $user->getInternalId()); - $this->assertEquals(1234, $user->getExternalId()); - $this->assertEquals('', $user->getRole()); - $this->assertEquals('', $user->getUsername()); - $this->assertEquals('test@localhost', $user->getEmail()); - $this->assertEquals('github_id', $user->getExternalIdColumn()); - $this->assertEquals(array(), $user->getExternalGroupIds()); - $this->assertEquals(array(), $user->getExtraAttributes()); - $this->assertFalse($user->isUserCreationAllowed()); - } - - public function testAuthenticationFailed() - { - $provider = $this - ->getMockBuilder('\Kanboard\Auth\GithubAuth') - ->setConstructorArgs(array($this->container)) - ->setMethods(array( - 'getProfile', - )) - ->getMock(); - - $provider->expects($this->once()) - ->method('getProfile') - ->will($this->returnValue(array())); - - $this->assertFalse($provider->authenticate()); - $this->assertEquals(null, $provider->getUser()); - } - - public function testGetService() - { - $provider = new GithubAuth($this->container); - $this->assertInstanceOf('Kanboard\Core\Http\OAuth2', $provider->getService()); - } - - public function testUnlink() - { - $userModel = new User($this->container); - $provider = new GithubAuth($this->container); - - $this->assertEquals(2, $userModel->create(array('username' => 'test', 'github_id' => '1234'))); - $this->assertNotEmpty($userModel->getByExternalId('github_id', 1234)); - - $this->assertTrue($provider->unlink(2)); - $this->assertEmpty($userModel->getByExternalId('github_id', 1234)); - } -} diff --git a/tests/units/Auth/GitlabAuthTest.php b/tests/units/Auth/GitlabAuthTest.php deleted file mode 100644 index e3ae0bdd..00000000 --- a/tests/units/Auth/GitlabAuthTest.php +++ /dev/null @@ -1,89 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Auth\GitlabAuth; -use Kanboard\Model\User; - -class GitlabAuthTest extends Base -{ - public function testGetName() - { - $provider = new GitlabAuth($this->container); - $this->assertEquals('Gitlab', $provider->getName()); - } - - public function testAuthenticationSuccessful() - { - $profile = array( - 'id' => 1234, - 'email' => 'test@localhost', - 'name' => 'Test', - ); - - $provider = $this - ->getMockBuilder('\Kanboard\Auth\GitlabAuth') - ->setConstructorArgs(array($this->container)) - ->setMethods(array( - 'getProfile', - )) - ->getMock(); - - $provider->expects($this->once()) - ->method('getProfile') - ->will($this->returnValue($profile)); - - $this->assertInstanceOf('Kanboard\Auth\GitlabAuth', $provider->setCode('1234')); - - $this->assertTrue($provider->authenticate()); - - $user = $provider->getUser(); - $this->assertInstanceOf('Kanboard\User\GitlabUserProvider', $user); - $this->assertEquals('Test', $user->getName()); - $this->assertEquals('', $user->getInternalId()); - $this->assertEquals(1234, $user->getExternalId()); - $this->assertEquals('', $user->getRole()); - $this->assertEquals('', $user->getUsername()); - $this->assertEquals('test@localhost', $user->getEmail()); - $this->assertEquals('gitlab_id', $user->getExternalIdColumn()); - $this->assertEquals(array(), $user->getExternalGroupIds()); - $this->assertEquals(array(), $user->getExtraAttributes()); - $this->assertFalse($user->isUserCreationAllowed()); - } - - public function testAuthenticationFailed() - { - $provider = $this - ->getMockBuilder('\Kanboard\Auth\GitlabAuth') - ->setConstructorArgs(array($this->container)) - ->setMethods(array( - 'getProfile', - )) - ->getMock(); - - $provider->expects($this->once()) - ->method('getProfile') - ->will($this->returnValue(array())); - - $this->assertFalse($provider->authenticate()); - $this->assertEquals(null, $provider->getUser()); - } - - public function testGetService() - { - $provider = new GitlabAuth($this->container); - $this->assertInstanceOf('Kanboard\Core\Http\OAuth2', $provider->getService()); - } - - public function testUnlink() - { - $userModel = new User($this->container); - $provider = new GitlabAuth($this->container); - - $this->assertEquals(2, $userModel->create(array('username' => 'test', 'gitlab_id' => '1234'))); - $this->assertNotEmpty($userModel->getByExternalId('gitlab_id', 1234)); - - $this->assertTrue($provider->unlink(2)); - $this->assertEmpty($userModel->getByExternalId('gitlab_id', 1234)); - } -} diff --git a/tests/units/Auth/GoogleAuthTest.php b/tests/units/Auth/GoogleAuthTest.php deleted file mode 100644 index b9a7d811..00000000 --- a/tests/units/Auth/GoogleAuthTest.php +++ /dev/null @@ -1,89 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Auth\GoogleAuth; -use Kanboard\Model\User; - -class GoogleAuthTest extends Base -{ - public function testGetName() - { - $provider = new GoogleAuth($this->container); - $this->assertEquals('Google', $provider->getName()); - } - - public function testAuthenticationSuccessful() - { - $profile = array( - 'id' => 1234, - 'email' => 'test@localhost', - 'name' => 'Test', - ); - - $provider = $this - ->getMockBuilder('\Kanboard\Auth\GoogleAuth') - ->setConstructorArgs(array($this->container)) - ->setMethods(array( - 'getProfile', - )) - ->getMock(); - - $provider->expects($this->once()) - ->method('getProfile') - ->will($this->returnValue($profile)); - - $this->assertInstanceOf('Kanboard\Auth\GoogleAuth', $provider->setCode('1234')); - - $this->assertTrue($provider->authenticate()); - - $user = $provider->getUser(); - $this->assertInstanceOf('Kanboard\User\GoogleUserProvider', $user); - $this->assertEquals('Test', $user->getName()); - $this->assertEquals('', $user->getInternalId()); - $this->assertEquals(1234, $user->getExternalId()); - $this->assertEquals('', $user->getRole()); - $this->assertEquals('', $user->getUsername()); - $this->assertEquals('test@localhost', $user->getEmail()); - $this->assertEquals('google_id', $user->getExternalIdColumn()); - $this->assertEquals(array(), $user->getExternalGroupIds()); - $this->assertEquals(array(), $user->getExtraAttributes()); - $this->assertFalse($user->isUserCreationAllowed()); - } - - public function testAuthenticationFailed() - { - $provider = $this - ->getMockBuilder('\Kanboard\Auth\GoogleAuth') - ->setConstructorArgs(array($this->container)) - ->setMethods(array( - 'getProfile', - )) - ->getMock(); - - $provider->expects($this->once()) - ->method('getProfile') - ->will($this->returnValue(array())); - - $this->assertFalse($provider->authenticate()); - $this->assertEquals(null, $provider->getUser()); - } - - public function testGetService() - { - $provider = new GoogleAuth($this->container); - $this->assertInstanceOf('Kanboard\Core\Http\OAuth2', $provider->getService()); - } - - public function testUnlink() - { - $userModel = new User($this->container); - $provider = new GoogleAuth($this->container); - - $this->assertEquals(2, $userModel->create(array('username' => 'test', 'google_id' => '1234'))); - $this->assertNotEmpty($userModel->getByExternalId('google_id', 1234)); - - $this->assertTrue($provider->unlink(2)); - $this->assertEmpty($userModel->getByExternalId('google_id', 1234)); - } -} diff --git a/tests/units/Base.php b/tests/units/Base.php index 4b54cdb0..bfcef418 100644 --- a/tests/units/Base.php +++ b/tests/units/Base.php @@ -10,6 +10,7 @@ use SimpleLogger\Logger; use SimpleLogger\File; use Kanboard\Core\Session\FlashMessage; use Kanboard\Core\Session\SessionStorage; +use Kanboard\ServiceProvider\ActionProvider; class FakeHttpClient { @@ -91,7 +92,11 @@ abstract class Base extends PHPUnit_Framework_TestCase $this->container['logger'] = new Logger; $this->container['logger']->setLogger(new File($this->isWindows() ? 'NUL' : '/dev/null')); $this->container['httpClient'] = new FakeHttpClient; - $this->container['emailClient'] = $this->getMockBuilder('EmailClient')->setMethods(array('send'))->getMock(); + $this->container['emailClient'] = $this + ->getMockBuilder('\Kanboard\Core\Mail\Client') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('send')) + ->getMock(); $this->container['userNotificationType'] = $this ->getMockBuilder('\Kanboard\Model\UserNotificationType') @@ -100,8 +105,9 @@ abstract class Base extends PHPUnit_Framework_TestCase ->getMock(); $this->container['sessionStorage'] = new SessionStorage; + $this->container->register(new ActionProvider); - $this->container['flash'] = function($c) { + $this->container['flash'] = function ($c) { return new FlashMessage($c); }; } diff --git a/tests/units/Core/ExternalLink/ExternalLinkManagerTest.php b/tests/units/Core/ExternalLink/ExternalLinkManagerTest.php new file mode 100644 index 00000000..d284a80b --- /dev/null +++ b/tests/units/Core/ExternalLink/ExternalLinkManagerTest.php @@ -0,0 +1,120 @@ +<?php + +require_once __DIR__.'/../../Base.php'; + +use Kanboard\Core\ExternalLink\ExternalLinkManager; +use Kanboard\ExternalLink\WebLinkProvider; +use Kanboard\ExternalLink\AttachmentLinkProvider; + +class ExternalLinkManagerTest extends Base +{ + public function testRegister() + { + $externalLinkManager = new ExternalLinkManager($this->container); + $webLinkProvider = new WebLinkProvider($this->container); + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + + $externalLinkManager->register($webLinkProvider); + $externalLinkManager->register($attachmentLinkProvider); + + $this->assertInstanceOf(get_class($webLinkProvider), $externalLinkManager->getProvider($webLinkProvider->getType())); + $this->assertInstanceOf(get_class($attachmentLinkProvider), $externalLinkManager->getProvider($attachmentLinkProvider->getType())); + } + + public function testGetProviderNotFound() + { + $externalLinkManager = new ExternalLinkManager($this->container); + + $this->setExpectedException('\Kanboard\Core\ExternalLink\ExternalLinkProviderNotFound'); + $externalLinkManager->getProvider('not found'); + } + + public function testGetTypes() + { + $externalLinkManager = new ExternalLinkManager($this->container); + $webLinkProvider = new WebLinkProvider($this->container); + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + + $this->assertEquals(array(ExternalLinkManager::TYPE_AUTO => 'Auto'), $externalLinkManager->getTypes()); + + $externalLinkManager->register($webLinkProvider); + $externalLinkManager->register($attachmentLinkProvider); + + $this->assertEquals( + array(ExternalLinkManager::TYPE_AUTO => 'Auto', 'attachment' => 'Attachment', 'weblink' => 'Web Link'), + $externalLinkManager->getTypes() + ); + } + + public function testGetDependencyLabel() + { + $externalLinkManager = new ExternalLinkManager($this->container); + $webLinkProvider = new WebLinkProvider($this->container); + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + + $externalLinkManager->register($webLinkProvider); + $externalLinkManager->register($attachmentLinkProvider); + + $this->assertSame('Related', $externalLinkManager->getDependencyLabel($webLinkProvider->getType(), 'related')); + $this->assertSame('custom', $externalLinkManager->getDependencyLabel($webLinkProvider->getType(), 'custom')); + } + + public function testFindProviderNotFound() + { + $externalLinkManager = new ExternalLinkManager($this->container); + $webLinkProvider = new WebLinkProvider($this->container); + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + + $externalLinkManager->register($webLinkProvider); + $externalLinkManager->register($attachmentLinkProvider); + + $this->setExpectedException('\Kanboard\Core\ExternalLink\ExternalLinkProviderNotFound'); + $externalLinkManager->find(); + } + + public function testFindProvider() + { + $externalLinkManager = new ExternalLinkManager($this->container); + $webLinkProvider = new WebLinkProvider($this->container); + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + + $externalLinkManager->register($webLinkProvider); + $externalLinkManager->register($attachmentLinkProvider); + + $externalLinkManager->setUserInput(array('text' => 'https://google.com/', 'type' => ExternalLinkManager::TYPE_AUTO)); + $this->assertSame($webLinkProvider, $externalLinkManager->find()); + + $externalLinkManager->setUserInput(array('text' => 'https://google.com/file.pdf', 'type' => ExternalLinkManager::TYPE_AUTO)); + $this->assertSame($attachmentLinkProvider, $externalLinkManager->find()); + } + + public function testFindProviderWithSelectedType() + { + $externalLinkManager = new ExternalLinkManager($this->container); + $webLinkProvider = new WebLinkProvider($this->container); + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + + $externalLinkManager->register($webLinkProvider); + $externalLinkManager->register($attachmentLinkProvider); + + $externalLinkManager->setUserInput(array('text' => 'https://google.com/', 'type' => $webLinkProvider->getType())); + $this->assertSame($webLinkProvider, $externalLinkManager->find()); + + $externalLinkManager->setUserInput(array('text' => 'https://google.com/file.pdf', 'type' => $attachmentLinkProvider->getType())); + $this->assertSame($attachmentLinkProvider, $externalLinkManager->find()); + } + + public function testFindProviderWithSelectedTypeNotFound() + { + $externalLinkManager = new ExternalLinkManager($this->container); + $webLinkProvider = new WebLinkProvider($this->container); + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + + $externalLinkManager->register($webLinkProvider); + $externalLinkManager->register($attachmentLinkProvider); + + $this->setExpectedException('\Kanboard\Core\ExternalLink\ExternalLinkProviderNotFound'); + $externalLinkManager->setUserInput(array('text' => 'https://google.com/', 'type' => 'not found')); + $externalLinkManager->find(); + } +} diff --git a/tests/units/ExternalLink/AttachmentLinkProviderTest.php b/tests/units/ExternalLink/AttachmentLinkProviderTest.php new file mode 100644 index 00000000..fe374664 --- /dev/null +++ b/tests/units/ExternalLink/AttachmentLinkProviderTest.php @@ -0,0 +1,64 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\ExternalLink\AttachmentLinkProvider; + +class AttachmentLinkProviderTest extends Base +{ + public function testGetName() + { + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + $this->assertEquals('Attachment', $attachmentLinkProvider->getName()); + } + + public function testGetType() + { + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + $this->assertEquals('attachment', $attachmentLinkProvider->getType()); + } + + public function testGetDependencies() + { + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + $this->assertEquals(array('related' => 'Related'), $attachmentLinkProvider->getDependencies()); + } + + public function testMatch() + { + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + + $attachmentLinkProvider->setUserTextInput('http://kanboard.net/FILE.DOC'); + $this->assertTrue($attachmentLinkProvider->match()); + + $attachmentLinkProvider->setUserTextInput('http://kanboard.net/folder/document.PDF'); + $this->assertTrue($attachmentLinkProvider->match()); + + $attachmentLinkProvider->setUserTextInput('http://kanboard.net/archive.zip'); + $this->assertTrue($attachmentLinkProvider->match()); + + $attachmentLinkProvider->setUserTextInput(' https://kanboard.net/folder/archive.tar '); + $this->assertTrue($attachmentLinkProvider->match()); + + $attachmentLinkProvider->setUserTextInput('http:// invalid url'); + $this->assertFalse($attachmentLinkProvider->match()); + + $attachmentLinkProvider->setUserTextInput(''); + $this->assertFalse($attachmentLinkProvider->match()); + + $attachmentLinkProvider->setUserTextInput('http://kanboard.net/folder/document.html'); + $this->assertFalse($attachmentLinkProvider->match()); + + $attachmentLinkProvider->setUserTextInput('http://kanboard.net/folder/DOC.HTML'); + $this->assertFalse($attachmentLinkProvider->match()); + + $attachmentLinkProvider->setUserTextInput('http://kanboard.net/folder/document.do'); + $this->assertFalse($attachmentLinkProvider->match()); + } + + public function testGetLink() + { + $attachmentLinkProvider = new AttachmentLinkProvider($this->container); + $this->assertInstanceOf('\Kanboard\ExternalLink\AttachmentLink', $attachmentLinkProvider->getLink()); + } +} diff --git a/tests/units/ExternalLink/AttachmentLinkTest.php b/tests/units/ExternalLink/AttachmentLinkTest.php new file mode 100644 index 00000000..0211869c --- /dev/null +++ b/tests/units/ExternalLink/AttachmentLinkTest.php @@ -0,0 +1,18 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\ExternalLink\AttachmentLink; + +class AttachmentLinkTest extends Base +{ + public function testGetTitleFromUrl() + { + $url = 'https://kanboard.net/folder/document.pdf'; + + $link = new AttachmentLink($this->container); + $link->setUrl($url); + $this->assertEquals($url, $link->getUrl()); + $this->assertEquals('document.pdf', $link->getTitle()); + } +} diff --git a/tests/units/ExternalLink/WebLinkProviderTest.php b/tests/units/ExternalLink/WebLinkProviderTest.php new file mode 100644 index 00000000..95110ed8 --- /dev/null +++ b/tests/units/ExternalLink/WebLinkProviderTest.php @@ -0,0 +1,52 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\ExternalLink\WebLinkProvider; + +class WebLinkProviderTest extends Base +{ + public function testGetName() + { + $webLinkProvider = new WebLinkProvider($this->container); + $this->assertEquals('Web Link', $webLinkProvider->getName()); + } + + public function testGetType() + { + $webLinkProvider = new WebLinkProvider($this->container); + $this->assertEquals('weblink', $webLinkProvider->getType()); + } + + public function testGetDependencies() + { + $webLinkProvider = new WebLinkProvider($this->container); + $this->assertEquals(array('related' => 'Related'), $webLinkProvider->getDependencies()); + } + + public function testMatch() + { + $webLinkProvider = new WebLinkProvider($this->container); + + $webLinkProvider->setUserTextInput('http://kanboard.net/'); + $this->assertTrue($webLinkProvider->match()); + + $webLinkProvider->setUserTextInput('http://kanboard.net/mypage'); + $this->assertTrue($webLinkProvider->match()); + + $webLinkProvider->setUserTextInput(' https://kanboard.net/ '); + $this->assertTrue($webLinkProvider->match()); + + $webLinkProvider->setUserTextInput('http:// invalid url'); + $this->assertFalse($webLinkProvider->match()); + + $webLinkProvider->setUserTextInput(''); + $this->assertFalse($webLinkProvider->match()); + } + + public function testGetLink() + { + $webLinkProvider = new WebLinkProvider($this->container); + $this->assertInstanceOf('\Kanboard\ExternalLink\WebLink', $webLinkProvider->getLink()); + } +} diff --git a/tests/units/ExternalLink/WebLinkTest.php b/tests/units/ExternalLink/WebLinkTest.php new file mode 100644 index 00000000..0644620f --- /dev/null +++ b/tests/units/ExternalLink/WebLinkTest.php @@ -0,0 +1,57 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\ExternalLink\WebLink; + +class WebLinkTest extends Base +{ + public function testGetTitleFromHtml() + { + $url = 'http://kanboard.net/something'; + $title = 'My title'; + $html = '<!DOCTYPE html><html><head><title> '.$title.' </title></head><body>Test</body></html>'; + + $this->container['httpClient'] = $this + ->getMockBuilder('\Kanboard\Core\Http\Client') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('get')) + ->getMock(); + + $webLink = new WebLink($this->container); + $webLink->setUrl($url); + $this->assertEquals($url, $webLink->getUrl()); + + $this->container['httpClient'] + ->expects($this->once()) + ->method('get') + ->with($url) + ->will($this->returnValue($html)); + + $this->assertEquals($title, $webLink->getTitle()); + } + + public function testGetTitleFromUrl() + { + $url = 'http://kanboard.net/something'; + $html = '<!DOCTYPE html><html><head></head><body>Test</body></html>'; + + $this->container['httpClient'] = $this + ->getMockBuilder('\Kanboard\Core\Http\Client') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('get')) + ->getMock(); + + $webLink = new WebLink($this->container); + $webLink->setUrl($url); + $this->assertEquals($url, $webLink->getUrl()); + + $this->container['httpClient'] + ->expects($this->once()) + ->method('get') + ->with($url) + ->will($this->returnValue($html)); + + $this->assertEquals('kanboard.net/something', $webLink->getTitle()); + } +} diff --git a/tests/units/Helper/UserHelperTest.php b/tests/units/Helper/UserHelperTest.php index 67ccee73..f1099faa 100644 --- a/tests/units/Helper/UserHelperTest.php +++ b/tests/units/Helper/UserHelperTest.php @@ -36,8 +36,8 @@ class UserHelperTest extends Base ); $this->assertTrue($helper->hasAccess('user', 'create')); - $this->assertTrue($helper->hasAccess('project', 'create')); - $this->assertTrue($helper->hasAccess('project', 'createPrivate')); + $this->assertTrue($helper->hasAccess('ProjectCreation', 'create')); + $this->assertTrue($helper->hasAccess('ProjectCreation', 'createPrivate')); } public function testHasAccessForManagers() @@ -50,8 +50,8 @@ class UserHelperTest extends Base ); $this->assertFalse($helper->hasAccess('user', 'create')); - $this->assertTrue($helper->hasAccess('project', 'create')); - $this->assertTrue($helper->hasAccess('project', 'createPrivate')); + $this->assertTrue($helper->hasAccess('ProjectCreation', 'create')); + $this->assertTrue($helper->hasAccess('ProjectCreation', 'createPrivate')); } public function testHasAccessForUsers() @@ -64,8 +64,8 @@ class UserHelperTest extends Base ); $this->assertFalse($helper->hasAccess('user', 'create')); - $this->assertFalse($helper->hasAccess('project', 'create')); - $this->assertTrue($helper->hasAccess('project', 'createPrivate')); + $this->assertFalse($helper->hasAccess('ProjectCreation', 'create')); + $this->assertTrue($helper->hasAccess('ProjectCreation', 'createPrivate')); } public function testHasProjectAccessForAdmins() diff --git a/tests/units/Model/ProjectDuplicationTest.php b/tests/units/Model/ProjectDuplicationTest.php index db5da525..ee5b4ce4 100644 --- a/tests/units/Model/ProjectDuplicationTest.php +++ b/tests/units/Model/ProjectDuplicationTest.php @@ -6,8 +6,11 @@ use Kanboard\Model\Action; use Kanboard\Model\Project; use Kanboard\Model\Category; use Kanboard\Model\ProjectUserRole; +use Kanboard\Model\ProjectGroupRole; use Kanboard\Model\ProjectDuplication; use Kanboard\Model\User; +use Kanboard\Model\Group; +use Kanboard\Model\GroupMember; use Kanboard\Model\Swimlane; use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; @@ -16,7 +19,14 @@ use Kanboard\Core\Security\Role; class ProjectDuplicationTest extends Base { - public function testProjectName() + public function testGetSelections() + { + $projectDuplicationModel = new ProjectDuplication($this->container); + $this->assertCount(5, $projectDuplicationModel->getOptionalSelection()); + $this->assertCount(6, $projectDuplicationModel->getPossibleSelection()); + } + + public function testGetClonedProjectName() { $pd = new ProjectDuplication($this->container); @@ -29,54 +39,142 @@ class ProjectDuplicationTest extends Base $this->assertEquals(str_repeat('a', 42).' (Clone)', $pd->getClonedProjectName(str_repeat('a', 60))); } - public function testCopyProjectWithLongName() + public function testClonePublicProject() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); - $this->assertEquals(1, $p->create(array('name' => str_repeat('a', 50)))); + $this->assertEquals(1, $p->create(array('name' => 'Public'))); $this->assertEquals(2, $pd->duplicate(1)); $project = $p->getById(2); $this->assertNotEmpty($project); - $this->assertEquals(str_repeat('a', 42).' (Clone)', $project['name']); + $this->assertEquals('Public (Clone)', $project['name']); + $this->assertEquals(1, $project['is_active']); + $this->assertEquals(0, $project['is_private']); + $this->assertEquals(0, $project['is_public']); + $this->assertEquals(0, $project['owner_id']); + $this->assertEmpty($project['token']); } - public function testClonePublicProject() + public function testClonePrivateProject() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); + $pp = new ProjectUserRole($this->container); - $this->assertEquals(1, $p->create(array('name' => 'Public'))); + $this->assertEquals(1, $p->create(array('name' => 'Private', 'is_private' => 1), 1, true)); $this->assertEquals(2, $pd->duplicate(1)); $project = $p->getById(2); $this->assertNotEmpty($project); - $this->assertEquals('Public (Clone)', $project['name']); - $this->assertEquals(0, $project['is_private']); + $this->assertEquals('Private (Clone)', $project['name']); + $this->assertEquals(1, $project['is_active']); + $this->assertEquals(1, $project['is_private']); $this->assertEquals(0, $project['is_public']); + $this->assertEquals(0, $project['owner_id']); $this->assertEmpty($project['token']); + + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 1)); } - public function testClonePrivateProject() + public function testCloneSharedProject() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); - $this->assertEquals(1, $p->create(array('name' => 'Private', 'is_private' => 1), 1, true)); + $this->assertEquals(1, $p->create(array('name' => 'Shared'))); + $this->assertTrue($p->update(array('id' => 1, 'is_public' => 1, 'token' => 'test'))); + + $project = $p->getById(1); + $this->assertEquals('test', $project['token']); + $this->assertEquals(1, $project['is_public']); + $this->assertEquals(2, $pd->duplicate(1)); $project = $p->getById(2); $this->assertNotEmpty($project); - $this->assertEquals('Private (Clone)', $project['name']); - $this->assertEquals(1, $project['is_private']); + $this->assertEquals('Shared (Clone)', $project['name']); + $this->assertEquals('', $project['token']); $this->assertEquals(0, $project['is_public']); - $this->assertEmpty($project['token']); + } - $pp = new ProjectUserRole($this->container); + public function testCloneInactiveProject() + { + $p = new Project($this->container); + $pd = new ProjectDuplication($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'Inactive'))); + $this->assertTrue($p->update(array('id' => 1, 'is_active' => 0))); + + $project = $p->getById(1); + $this->assertEquals(0, $project['is_active']); + + $this->assertEquals(2, $pd->duplicate(1)); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Inactive (Clone)', $project['name']); + $this->assertEquals(1, $project['is_active']); + } + + public function testCloneProjectWithOwner() + { + $p = new Project($this->container); + $pd = new ProjectDuplication($this->container); + $projectUserRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'Owner'))); + + $project = $p->getById(1); + $this->assertEquals(0, $project['owner_id']); + + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 1)); - $this->assertEquals(array(1 => 'admin'), $pp->getAssignableUsers(1)); - $this->assertEquals(array(1 => 'admin'), $pp->getAssignableUsers(2)); + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Owner (Clone)', $project['name']); + $this->assertEquals(1, $project['owner_id']); + + $this->assertEquals(Role::PROJECT_MANAGER, $projectUserRoleModel->getUserRole(2, 1)); + } + + public function testCloneProjectWithDifferentName() + { + $p = new Project($this->container); + $pd = new ProjectDuplication($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'Owner'))); + + $project = $p->getById(1); + $this->assertEquals(0, $project['owner_id']); + + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 1, 'Foobar')); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Foobar', $project['name']); + $this->assertEquals(1, $project['owner_id']); + } + + public function testCloneProjectAndForceItToBePrivate() + { + $p = new Project($this->container); + $pd = new ProjectDuplication($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'Owner'))); + + $project = $p->getById(1); + $this->assertEquals(0, $project['owner_id']); + $this->assertEquals(0, $project['is_private']); + + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 1, 'Foobar', true)); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Foobar', $project['name']); + $this->assertEquals(1, $project['owner_id']); + $this->assertEquals(1, $project['is_private']); } public function testCloneProjectWithCategories() @@ -98,16 +196,9 @@ class ProjectDuplicationTest extends Base $this->assertEquals('P1 (Clone)', $project['name']); $categories = $c->getAll(2); - $this->assertNotempty($categories); - $this->assertEquals(3, count($categories)); - - $this->assertEquals(4, $categories[0]['id']); + $this->assertCount(3, $categories); $this->assertEquals('C1', $categories[0]['name']); - - $this->assertEquals(5, $categories[1]['id']); $this->assertEquals('C2', $categories[1]['name']); - - $this->assertEquals(6, $categories[2]['id']); $this->assertEquals('C3', $categories[2]['name']); } @@ -119,28 +210,115 @@ class ProjectDuplicationTest extends Base $u = new User($this->container); $pd = new ProjectDuplication($this->container); - $this->assertEquals(2, $u->create(array('username' => 'unittest1', 'password' => 'unittest'))); - $this->assertEquals(3, $u->create(array('username' => 'unittest2', 'password' => 'unittest'))); - $this->assertEquals(4, $u->create(array('username' => 'unittest3', 'password' => 'unittest'))); + $this->assertEquals(2, $u->create(array('username' => 'user1'))); + $this->assertEquals(3, $u->create(array('username' => 'user2'))); + $this->assertEquals(4, $u->create(array('username' => 'user3'))); $this->assertEquals(1, $p->create(array('name' => 'P1'))); - $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MEMBER)); + + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MANAGER)); $this->assertTrue($pp->addUser(1, 3, Role::PROJECT_MEMBER)); - $this->assertTrue($pp->addUser(1, 4, Role::PROJECT_MANAGER)); - $this->assertEquals(Role::PROJECT_MEMBER, $pp->getUserRole(1, 2)); - $this->assertEquals(Role::PROJECT_MEMBER, $pp->getUserRole(1, 3)); - $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(1, 4)); + $this->assertTrue($pp->addUser(1, 4, Role::PROJECT_VIEWER)); $this->assertEquals(2, $pd->duplicate(1)); + $this->assertCount(3, $pp->getUsers(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 2)); + $this->assertEquals(Role::PROJECT_MEMBER, $pp->getUserRole(2, 3)); + $this->assertEquals(Role::PROJECT_VIEWER, $pp->getUserRole(2, 4)); + } + + public function testCloneProjectWithUsersAndOverrideOwner() + { + $p = new Project($this->container); + $c = new Category($this->container); + $pp = new ProjectUserRole($this->container); + $u = new User($this->container); + $pd = new ProjectDuplication($this->container); + + $this->assertEquals(2, $u->create(array('username' => 'user1'))); + $this->assertEquals(1, $p->create(array('name' => 'P1'), 2)); + + $project = $p->getById(1); + $this->assertEquals(2, $project['owner_id']); + + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($pp->addUser(1, 1, Role::PROJECT_MEMBER)); + + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 1)); + + $this->assertCount(2, $pp->getUsers(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 2)); + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 1)); + $project = $p->getById(2); - $this->assertNotEmpty($project); - $this->assertEquals('P1 (Clone)', $project['name']); + $this->assertEquals(1, $project['owner_id']); + } - $this->assertEquals(3, count($pp->getUsers(2))); - $this->assertEquals(Role::PROJECT_MEMBER, $pp->getUserRole(2, 2)); - $this->assertEquals(Role::PROJECT_MEMBER, $pp->getUserRole(2, 3)); - $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 4)); + public function testCloneTeamProjectToPrivatProject() + { + $p = new Project($this->container); + $c = new Category($this->container); + $pp = new ProjectUserRole($this->container); + $u = new User($this->container); + $pd = new ProjectDuplication($this->container); + + $this->assertEquals(2, $u->create(array('username' => 'user1'))); + $this->assertEquals(3, $u->create(array('username' => 'user2'))); + $this->assertEquals(1, $p->create(array('name' => 'P1'), 2)); + + $project = $p->getById(1); + $this->assertEquals(2, $project['owner_id']); + $this->assertEquals(0, $project['is_private']); + + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($pp->addUser(1, 1, Role::PROJECT_MEMBER)); + + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 3, 'My private project', true)); + + $this->assertCount(1, $pp->getUsers(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 3)); + + $project = $p->getById(2); + $this->assertEquals(3, $project['owner_id']); + $this->assertEquals(1, $project['is_private']); + } + + public function testCloneProjectWithGroups() + { + $p = new Project($this->container); + $c = new Category($this->container); + $pd = new ProjectDuplication($this->container); + $userModel = new User($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $projectGroupRoleModel = new ProjectGroupRole($this->container); + $projectUserRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'P1'))); + + $this->assertEquals(1, $groupModel->create('G1')); + $this->assertEquals(2, $groupModel->create('G2')); + $this->assertEquals(3, $groupModel->create('G3')); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user3'))); + + $this->assertTrue($groupMemberModel->addUser(1, 2)); + $this->assertTrue($groupMemberModel->addUser(2, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 4)); + + $this->assertTrue($projectGroupRoleModel->addGroup(1, 1, Role::PROJECT_MANAGER)); + $this->assertTrue($projectGroupRoleModel->addGroup(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($projectGroupRoleModel->addGroup(1, 3, Role::PROJECT_VIEWER)); + + $this->assertEquals(2, $pd->duplicate(1)); + + $this->assertCount(3, $projectGroupRoleModel->getGroups(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $projectUserRoleModel->getUserRole(2, 2)); + $this->assertEquals(Role::PROJECT_MEMBER, $projectUserRoleModel->getUserRole(2, 3)); + $this->assertEquals(Role::PROJECT_VIEWER, $projectUserRoleModel->getUserRole(2, 4)); } public function testCloneProjectWithActionTaskAssignCurrentUser() @@ -199,7 +377,7 @@ class ProjectDuplicationTest extends Base $this->assertEquals(5, $actions[0]['params']['category_id']); } - public function testCloneProjectWithSwimlanesAndTasks() + public function testCloneProjectWithSwimlanes() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); @@ -207,31 +385,22 @@ class ProjectDuplicationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); - $this->assertEquals(1, $p->create(array('name' => 'P1'))); + $this->assertEquals(1, $p->create(array('name' => 'P1', 'default_swimlane' => 'New Default'))); // create initial swimlanes $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'S1'))); $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'S2'))); $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'S3'))); - $default_swimlane1 = $s->getDefault(1); - $default_swimlane1['default_swimlane'] = 'New Default'; - - $this->assertTrue($s->updateDefault($default_swimlane1)); + // create initial tasks + $this->assertEquals(1, $tc->create(array('title' => 'T0', 'project_id' => 1, 'swimlane_id' => 0))); + $this->assertEquals(2, $tc->create(array('title' => 'T1', 'project_id' => 1, 'swimlane_id' => 1))); + $this->assertEquals(3, $tc->create(array('title' => 'T2', 'project_id' => 1, 'swimlane_id' => 2))); + $this->assertEquals(4, $tc->create(array('title' => 'T3', 'project_id' => 1, 'swimlane_id' => 3))); - //create initial tasks - $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); - $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1))); - $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - - $this->assertNotFalse($pd->duplicate(1, array('category', 'action', 'swimlane', 'task'))); - $project = $p->getByName('P1 (Clone)'); - $this->assertNotFalse($project); - $project_id = $project['id']; - - // Check if Swimlanes have been duplicated - $swimlanes = $s->getAll($project_id); + $this->assertEquals(2, $pd->duplicate(1, array('category', 'swimlane'))); + $swimlanes = $s->getAll(2); $this->assertCount(3, $swimlanes); $this->assertEquals(4, $swimlanes[0]['id']); $this->assertEquals('S1', $swimlanes[0]['name']); @@ -239,29 +408,15 @@ class ProjectDuplicationTest extends Base $this->assertEquals('S2', $swimlanes[1]['name']); $this->assertEquals(6, $swimlanes[2]['id']); $this->assertEquals('S3', $swimlanes[2]['name']); - $new_default = $s->getDefault($project_id); - $this->assertEquals('New Default', $new_default['default_swimlane']); - - // Check if Tasks have been duplicated - $tasks = $tf->getAll($project_id); + $swimlane = $s->getDefault(2); + $this->assertEquals('New Default', $swimlane['default_swimlane']); - $this->assertCount(3, $tasks); - // $this->assertEquals(4, $tasks[0]['id']); - $this->assertEquals('T1', $tasks[0]['title']); - // $this->assertEquals(5, $tasks[1]['id']); - $this->assertEquals('T2', $tasks[1]['title']); - // $this->assertEquals(6, $tasks[2]['id']); - $this->assertEquals('T3', $tasks[2]['title']); - - $p->remove($project_id); - - $this->assertFalse($p->exists($project_id)); - $this->assertCount(0, $s->getAll($project_id)); - $this->assertCount(0, $tf->getAll($project_id)); + // Check if tasks are NOT been duplicated + $this->assertCount(0, $tf->getAll(2)); } - public function testCloneProjectWithSwimlanes() + public function testCloneProjectWithTasks() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); @@ -271,43 +426,22 @@ class ProjectDuplicationTest extends Base $this->assertEquals(1, $p->create(array('name' => 'P1'))); - // create initial swimlanes - $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'S1'))); - $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'S2'))); - $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'S3'))); + // create initial tasks + $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2))); + $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3))); - $default_swimlane1 = $s->getDefault(1); - $default_swimlane1['default_swimlane'] = 'New Default'; - - $this->assertTrue($s->updateDefault($default_swimlane1)); - - //create initial tasks - $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); - $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1))); - $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - - $this->assertNotFalse($pd->duplicate(1, array('category', 'action', 'swimlane'))); - $project = $p->getByName('P1 (Clone)'); - $this->assertNotFalse($project); - $project_id = $project['id']; - - $swimlanes = $s->getAll($project_id); - - $this->assertCount(3, $swimlanes); - $this->assertEquals(4, $swimlanes[0]['id']); - $this->assertEquals('S1', $swimlanes[0]['name']); - $this->assertEquals(5, $swimlanes[1]['id']); - $this->assertEquals('S2', $swimlanes[1]['name']); - $this->assertEquals(6, $swimlanes[2]['id']); - $this->assertEquals('S3', $swimlanes[2]['name']); - $new_default = $s->getDefault($project_id); - $this->assertEquals('New Default', $new_default['default_swimlane']); + $this->assertEquals(2, $pd->duplicate(1, array('category', 'action', 'task'))); - // Check if Tasks have NOT been duplicated - $this->assertCount(0, $tf->getAll($project_id)); + // Check if Tasks have been duplicated + $tasks = $tf->getAll(2); + $this->assertCount(3, $tasks); + $this->assertEquals('T1', $tasks[0]['title']); + $this->assertEquals('T2', $tasks[1]['title']); + $this->assertEquals('T3', $tasks[2]['title']); } - public function testCloneProjectWithTasks() + public function testCloneProjectWithSwimlanesAndTasks() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); @@ -315,40 +449,39 @@ class ProjectDuplicationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); - $this->assertEquals(1, $p->create(array('name' => 'P1'))); + $this->assertEquals(1, $p->create(array('name' => 'P1', 'default_swimlane' => 'New Default'))); // create initial swimlanes $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'S1'))); $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'S2'))); $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'S3'))); - $default_swimlane1 = $s->getDefault(1); - $default_swimlane1['default_swimlane'] = 'New Default'; - - $this->assertTrue($s->updateDefault($default_swimlane1)); - - //create initial tasks + // create initial tasks $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1))); $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - $this->assertNotFalse($pd->duplicate(1, array('category', 'action', 'task'))); - $project = $p->getByName('P1 (Clone)'); - $this->assertNotFalse($project); - $project_id = $project['id']; + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission', 'swimlane', 'task'))); + + // Check if Swimlanes have been duplicated + $swimlanes = $s->getAll(2); + $this->assertCount(3, $swimlanes); + $this->assertEquals(4, $swimlanes[0]['id']); + $this->assertEquals('S1', $swimlanes[0]['name']); + $this->assertEquals(5, $swimlanes[1]['id']); + $this->assertEquals('S2', $swimlanes[1]['name']); + $this->assertEquals(6, $swimlanes[2]['id']); + $this->assertEquals('S3', $swimlanes[2]['name']); - // Check if Swimlanes have NOT been duplicated - $this->assertCount(0, $s->getAll($project_id)); + $swimlane = $s->getDefault(2); + $this->assertEquals('New Default', $swimlane['default_swimlane']); // Check if Tasks have been duplicated - $tasks = $tf->getAll($project_id); + $tasks = $tf->getAll(2); $this->assertCount(3, $tasks); - //$this->assertEquals(4, $tasks[0]['id']); $this->assertEquals('T1', $tasks[0]['title']); - //$this->assertEquals(5, $tasks[1]['id']); $this->assertEquals('T2', $tasks[1]['title']); - //$this->assertEquals(6, $tasks[2]['id']); $this->assertEquals('T3', $tasks[2]['title']); } } diff --git a/tests/units/Model/SubtaskTest.php b/tests/units/Model/SubtaskTest.php index 9be2dff4..34599004 100644 --- a/tests/units/Model/SubtaskTest.php +++ b/tests/units/Model/SubtaskTest.php @@ -159,7 +159,7 @@ class SubtaskTest extends Base $this->assertEquals(0, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_INPROGRESS, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -167,7 +167,7 @@ class SubtaskTest extends Base $this->assertEquals(0, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_DONE, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -175,7 +175,7 @@ class SubtaskTest extends Base $this->assertEquals(0, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_TODO, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -205,7 +205,7 @@ class SubtaskTest extends Base // Set the current logged user $this->container['sessionStorage']->user = array('id' => 1); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_INPROGRESS, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -213,7 +213,7 @@ class SubtaskTest extends Base $this->assertEquals(1, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_DONE, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -221,7 +221,7 @@ class SubtaskTest extends Base $this->assertEquals(1, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_TODO, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); diff --git a/tests/units/Model/TaskExternalLinkTest.php b/tests/units/Model/TaskExternalLinkTest.php new file mode 100644 index 00000000..28ccab83 --- /dev/null +++ b/tests/units/Model/TaskExternalLinkTest.php @@ -0,0 +1,123 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\TaskCreation; +use Kanboard\Model\Project; +use Kanboard\Model\TaskExternalLink; +use Kanboard\Core\ExternalLink\ExternalLinkManager; +use Kanboard\ExternalLink\WebLinkProvider; + +class TaskExternalLinkTest extends Base +{ + public function testCreate() + { + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'id' => '', 'url' => 'http://kanboard.net/', 'title' => 'My website', 'link_type' => 'weblink', 'dependency' => 'related'))); + + $link = $taskExternalLinkModel->getById(1); + $this->assertNotEmpty($link); + $this->assertEquals('My website', $link['title']); + $this->assertEquals('http://kanboard.net/', $link['url']); + $this->assertEquals('related', $link['dependency']); + $this->assertEquals('weblink', $link['link_type']); + $this->assertEquals(0, $link['creator_id']); + $this->assertEquals(time(), $link['date_modification'], '', 2); + $this->assertEquals(time(), $link['date_creation'], '', 2); + } + + public function testCreateWithUserSession() + { + $this->container['sessionStorage']->user = array('id' => 1); + + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'id' => '', 'url' => 'http://kanboard.net/', 'title' => 'My website', 'link_type' => 'weblink', 'dependency' => 'related'))); + + $link = $taskExternalLinkModel->getById(1); + $this->assertNotEmpty($link); + $this->assertEquals('My website', $link['title']); + $this->assertEquals('http://kanboard.net/', $link['url']); + $this->assertEquals('related', $link['dependency']); + $this->assertEquals('weblink', $link['link_type']); + $this->assertEquals(1, $link['creator_id']); + $this->assertEquals(time(), $link['date_modification'], '', 2); + $this->assertEquals(time(), $link['date_creation'], '', 2); + } + + public function testModification() + { + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'id' => '', 'url' => 'http://kanboard.net/', 'title' => 'My website', 'link_type' => 'weblink', 'dependency' => 'related'))); + + sleep(1); + + $this->assertTrue($taskExternalLinkModel->update(array('id' => 1, 'url' => 'https://kanboard.net/'))); + + $link = $taskExternalLinkModel->getById(1); + $this->assertNotEmpty($link); + $this->assertEquals('https://kanboard.net/', $link['url']); + $this->assertEquals(time(), $link['date_modification'], '', 2); + } + + public function testRemove() + { + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'id' => '', 'url' => 'http://kanboard.net/', 'title' => 'My website', 'link_type' => 'weblink', 'dependency' => 'related'))); + + $this->assertTrue($taskExternalLinkModel->remove(1)); + $this->assertFalse($taskExternalLinkModel->remove(1)); + + $this->assertEmpty($taskExternalLinkModel->getById(1)); + } + + public function testGetAll() + { + $this->container['sessionStorage']->user = array('id' => 1); + $this->container['externalLinkManager'] = new ExternalLinkManager($this->container); + + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + $webLinkProvider = new WebLinkProvider($this->container); + + $this->container['externalLinkManager']->register($webLinkProvider); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'url' => 'https://miniflux.net/', 'title' => 'MX', 'link_type' => 'weblink', 'dependency' => 'related'))); + $this->assertEquals(2, $taskExternalLinkModel->create(array('task_id' => 1, 'url' => 'http://kanboard.net/', 'title' => 'KB', 'link_type' => 'weblink', 'dependency' => 'related'))); + + $links = $taskExternalLinkModel->getAll(1); + $this->assertCount(2, $links); + $this->assertEquals('KB', $links[0]['title']); + $this->assertEquals('MX', $links[1]['title']); + $this->assertEquals('Web Link', $links[0]['type']); + $this->assertEquals('Web Link', $links[1]['type']); + $this->assertEquals('Related', $links[0]['dependency_label']); + $this->assertEquals('Related', $links[1]['dependency_label']); + $this->assertEquals('admin', $links[0]['creator_username']); + $this->assertEquals('admin', $links[1]['creator_username']); + $this->assertEquals('', $links[0]['creator_name']); + $this->assertEquals('', $links[1]['creator_name']); + } +} diff --git a/tests/units/Validator/ExternalLinkValidatorTest.php b/tests/units/Validator/ExternalLinkValidatorTest.php new file mode 100644 index 00000000..b41b779a --- /dev/null +++ b/tests/units/Validator/ExternalLinkValidatorTest.php @@ -0,0 +1,63 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Validator\ExternalLinkValidator; + +class ExternalLinkValidatorTest extends Base +{ + public function testValidateCreation() + { + $validator = new ExternalLinkValidator($this->container); + + $result = $validator->validateCreation(array('url' => 'http://somewhere', 'task_id' => 1, 'title' => 'Title', 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertTrue($result[0]); + + $result = $validator->validateCreation(array('url' => 'http://somewhere', 'task_id' => 'abc', 'title' => 'Title', 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertFalse($result[0]); + + $result = $validator->validateCreation(array('url' => 'http://somewhere', 'task_id' => 1, 'title' => 'Title', 'link_type' => 'weblink')); + $this->assertFalse($result[0]); + + $result = $validator->validateCreation(array('url' => 'http://somewhere', 'task_id' => 1, 'title' => 'Title', 'dependency' => 'related')); + $this->assertFalse($result[0]); + + $result = $validator->validateCreation(array('url' => 'http://somewhere', 'task_id' => 1, 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertFalse($result[0]); + + $result = $validator->validateCreation(array('url' => 'http://somewhere', 'title' => 'Title', 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertFalse($result[0]); + + $result = $validator->validateCreation(array('task_id' => 1, 'title' => 'Title', 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertFalse($result[0]); + } + + public function testValidateModification() + { + $validator = new ExternalLinkValidator($this->container); + + $result = $validator->validateModification(array('id' => 1, 'url' => 'http://somewhere', 'task_id' => 1, 'title' => 'Title', 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertTrue($result[0]); + + $result = $validator->validateModification(array('id' => 1, 'url' => 'http://somewhere', 'task_id' => 'abc', 'title' => 'Title', 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertFalse($result[0]); + + $result = $validator->validateModification(array('id' => 1, 'url' => 'http://somewhere', 'task_id' => 1, 'title' => 'Title', 'link_type' => 'weblink')); + $this->assertFalse($result[0]); + + $result = $validator->validateModification(array('id' => 1, 'url' => 'http://somewhere', 'task_id' => 1, 'title' => 'Title', 'dependency' => 'related')); + $this->assertFalse($result[0]); + + $result = $validator->validateModification(array('id' => 1, 'url' => 'http://somewhere', 'task_id' => 1, 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertFalse($result[0]); + + $result = $validator->validateModification(array('id' => 1, 'url' => 'http://somewhere', 'title' => 'Title', 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertFalse($result[0]); + + $result = $validator->validateModification(array('id' => 1, 'task_id' => 1, 'title' => 'Title', 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertFalse($result[0]); + + $result = $validator->validateModification(array('url' => 'http://somewhere', 'task_id' => 1, 'title' => 'Title', 'link_type' => 'weblink', 'dependency' => 'related')); + $this->assertFalse($result[0]); + } +} |