diff options
Diffstat (limited to 'tests/units/ProjectTest.php')
-rw-r--r-- | tests/units/ProjectTest.php | 173 |
1 files changed, 138 insertions, 35 deletions
diff --git a/tests/units/ProjectTest.php b/tests/units/ProjectTest.php index cec8d93d..fec53c2b 100644 --- a/tests/units/ProjectTest.php +++ b/tests/units/ProjectTest.php @@ -2,18 +2,36 @@ require_once __DIR__.'/Base.php'; +use Core\Translator; +use Subscriber\ProjectModificationDateSubscriber; use Model\Project; use Model\ProjectPermission; use Model\User; use Model\Task; +use Model\TaskCreation; use Model\Acl; use Model\Board; +use Model\Config; +use Model\Category; class ProjectTest extends Base { + public function testCreationForAllLanguages() + { + $c = new Config($this->container); + $p = new Project($this->container); + + foreach ($c->getLanguages() as $locale => $language) { + Translator::load($locale); + $this->assertNotFalse($p->create(array('name' => 'UnitTest '.$locale)), 'Unable to create project with '.$locale.':'.$language); + } + + Translator::load('en_US'); + } + public function testCreation() { - $p = new Project($this->registry); + $p = new Project($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); @@ -26,32 +44,89 @@ class ProjectTest extends Base $this->assertEmpty($project['token']); } + public function testCreationWithDefaultCategories() + { + $p = new Project($this->container); + $c = new Config($this->container); + $cat = new Category($this->container); + + // Multiple categories correctly formatted + + $this->assertTrue($c->save(array('project_categories' => 'Test1, Test2'))); + $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); + + $project = $p->getById(1); + $this->assertNotEmpty($project); + + $categories = $cat->getAll(1); + $this->assertNotEmpty($categories); + $this->assertEquals(2, count($categories)); + $this->assertEquals('Test1', $categories[0]['name']); + $this->assertEquals('Test2', $categories[1]['name']); + + // Single category + + $this->assertTrue($c->save(array('project_categories' => 'Test1'))); + $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + + $categories = $cat->getAll(2); + $this->assertNotEmpty($categories); + $this->assertEquals(1, count($categories)); + $this->assertEquals('Test1', $categories[0]['name']); + + // Multiple categories badly formatted + + $this->assertTrue($c->save(array('project_categories' => 'ABC, , DEF 3, '))); + $this->assertEquals(3, $p->create(array('name' => 'UnitTest3'))); + + $project = $p->getById(3); + $this->assertNotEmpty($project); + + $categories = $cat->getAll(3); + $this->assertNotEmpty($categories); + $this->assertEquals(2, count($categories)); + $this->assertEquals('ABC', $categories[0]['name']); + $this->assertEquals('DEF 3', $categories[1]['name']); + + // No default categories + $this->assertTrue($c->save(array('project_categories' => ' '))); + $this->assertEquals(4, $p->create(array('name' => 'UnitTest4'))); + + $project = $p->getById(4); + $this->assertNotEmpty($project); + + $categories = $cat->getAll(4); + $this->assertEmpty($categories); + } + public function testUpdateLastModifiedDate() { - $p = new Project($this->registry); + $p = new Project($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); $now = time(); $project = $p->getById(1); $this->assertNotEmpty($project); - $this->assertEquals($now, $project['last_modified']); + $this->assertEquals($now, $project['last_modified'], 'Wrong Timestamp', 1); sleep(1); $this->assertTrue($p->updateModificationDate(1)); $project = $p->getById(1); $this->assertNotEmpty($project); - $this->assertEquals($now + 1, $project['last_modified']); + $this->assertGreaterThan($now, $project['last_modified']); } public function testIsLastModified() { - $p = new Project($this->registry); - $t = new Task($this->registry); + $p = new Project($this->container); + $tc = new TaskCreation($this->container); $now = time(); - $p->attachEvents(); $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); @@ -61,9 +136,13 @@ class ProjectTest extends Base sleep(1); - $this->assertEquals(1, $t->create(array('title' => 'Task #1', 'project_id' => 1))); - $this->assertTrue($this->registry->shared('event')->isEventTriggered(Task::EVENT_CREATE)); - $this->assertEquals('Event\ProjectModificationDateListener', $this->registry->shared('event')->getLastListenerExecuted()); + $listener = new ProjectModificationDateSubscriber($this->container); + $this->container['dispatcher']->addListener(Task::EVENT_CREATE_UPDATE, array($listener, 'execute')); + + $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1))); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(Task::EVENT_CREATE_UPDATE.'.Subscriber\ProjectModificationDateSubscriber::execute', $called); $project = $p->getById(1); $this->assertNotEmpty($project); @@ -72,7 +151,7 @@ class ProjectTest extends Base public function testRemove() { - $p = new Project($this->registry); + $p = new Project($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); $this->assertTrue($p->remove(1)); @@ -81,7 +160,7 @@ class ProjectTest extends Base public function testEnable() { - $p = new Project($this->registry); + $p = new Project($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); $this->assertTrue($p->disable(1)); @@ -95,7 +174,7 @@ class ProjectTest extends Base public function testDisable() { - $p = new Project($this->registry); + $p = new Project($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); $this->assertTrue($p->disable(1)); @@ -110,7 +189,7 @@ class ProjectTest extends Base public function testEnablePublicAccess() { - $p = new Project($this->registry); + $p = new Project($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); $this->assertTrue($p->enablePublicAccess(1)); @@ -125,7 +204,7 @@ class ProjectTest extends Base public function testDisablePublicAccess() { - $p = new Project($this->registry); + $p = new Project($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); $this->assertTrue($p->enablePublicAccess(1)); @@ -139,35 +218,59 @@ class ProjectTest extends Base $this->assertFalse($p->disablePublicAccess(123)); } - public function testDuplicate() + public function testIdentifier() { - $p = new Project($this->registry); + $p = new Project($this->container); - // Clone public project - $this->assertEquals(1, $p->create(array('name' => 'Public'))); - $this->assertEquals(2, $p->duplicate(1)); + // Creation + $this->assertEquals(1, $p->create(array('name' => 'UnitTest1', 'identifier' => 'test1'))); + $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); + + $project = $p->getById(1); + $this->assertNotEmpty($project); + $this->assertEquals('TEST1', $project['identifier']); $project = $p->getById(2); $this->assertNotEmpty($project); - $this->assertEquals('Public (Clone)', $project['name']); - $this->assertEquals(0, $project['is_private']); - $this->assertEquals(0, $project['is_public']); - $this->assertEmpty($project['token']); + $this->assertEquals('', $project['identifier']); - // Clone private project - $this->assertEquals(3, $p->create(array('name' => 'Private', 'is_private' => 1), 1)); - $this->assertEquals(4, $p->duplicate(3)); + // Update + $this->assertTrue($p->update(array('id' => '2', 'identifier' => 'test2'))); - $project = $p->getById(4); + $project = $p->getById(2); $this->assertNotEmpty($project); - $this->assertEquals('Private (Clone)', $project['name']); - $this->assertEquals(1, $project['is_private']); - $this->assertEquals(0, $project['is_public']); - $this->assertEmpty($project['token']); + $this->assertEquals('TEST2', $project['identifier']); + + $project = $p->getByIdentifier('test1'); + $this->assertNotEmpty($project); + $this->assertEquals('TEST1', $project['identifier']); + + $project = $p->getByIdentifier(''); + $this->assertFalse($project); + + // Validation rules + $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'TEST1')); + $this->assertFalse($r[0]); + + $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'test1')); + $this->assertFalse($r[0]); + + $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST1')); + $this->assertTrue($r[0]); + + $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'test3')); + $this->assertTrue($r[0]); + + $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => '')); + $this->assertTrue($r[0]); + + $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST2')); + $this->assertFalse($r[0]); - $pp = new ProjectPermission($this->registry); + $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'a-b-c')); + $this->assertFalse($r[0]); - $this->assertEquals(array(1 => 'admin'), $pp->getAllowedUsers(3)); - $this->assertEquals(array(1 => 'admin'), $pp->getAllowedUsers(4)); + $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'test 123')); + $this->assertFalse($r[0]); } } |