From cb0916d10e4a42a62f0ac8c69ecb4b7a15f398b4 Mon Sep 17 00:00:00 2001
From: Frederic Guillot <fred@kanboard.net>
Date: Sat, 20 Jun 2015 10:48:47 -0400
Subject: Add automatic action to send a task by email

---
 tests/units/ActionTaskEmailTest.php           | 149 ++++++++++++++++++++++++++
 tests/units/ActionTaskUpdateStartDateTest.php |   1 -
 tests/units/Base.php                          |  18 +---
 tests/units/ConfigTest.php                    |   8 ++
 tests/units/NotificationTest.php              |  22 ++--
 tests/units/ProjectTest.php                   |   2 +-
 6 files changed, 172 insertions(+), 28 deletions(-)
 create mode 100644 tests/units/ActionTaskEmailTest.php

(limited to 'tests/units')

diff --git a/tests/units/ActionTaskEmailTest.php b/tests/units/ActionTaskEmailTest.php
new file mode 100644
index 00000000..adc74512
--- /dev/null
+++ b/tests/units/ActionTaskEmailTest.php
@@ -0,0 +1,149 @@
+<?php
+
+require_once __DIR__.'/Base.php';
+
+use Event\GenericEvent;
+use Model\Task;
+use Model\TaskCreation;
+use Model\TaskFinder;
+use Model\Project;
+use Model\User;
+
+class ActionTaskEmailTest extends Base
+{
+    public function testNoEmail()
+    {
+        $action = new Action\TaskEmail($this->container, 1, Task::EVENT_MOVE_COLUMN);
+        $action->setParam('column_id', 2);
+        $action->setParam('user_id', 1);
+        $action->setParam('subject', 'My email subject');
+
+        // We create a task in the first column
+        $tc = new TaskCreation($this->container);
+        $tf = new TaskFinder($this->container);
+        $p = new Project($this->container);
+        $u = new User($this->container);
+
+        $this->assertEquals(1, $p->create(array('name' => 'test')));
+        $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
+
+        // We create an event to move the task to the 2nd column
+        $event = array(
+            'project_id' => 1,
+            'task_id' => 1,
+            'column_id' => 2,
+        );
+
+        // Email should be not be sent
+        $this->container['emailClient']->expects($this->never())->method('send');
+
+        // Our event should be executed
+        $this->assertFalse($action->execute(new GenericEvent($event)));
+    }
+
+    public function testWrongColumn()
+    {
+        $action = new Action\TaskEmail($this->container, 1, Task::EVENT_MOVE_COLUMN);
+        $action->setParam('column_id', 2);
+        $action->setParam('user_id', 1);
+        $action->setParam('subject', 'My email subject');
+
+        // We create a task in the first column
+        $tc = new TaskCreation($this->container);
+        $tf = new TaskFinder($this->container);
+        $p = new Project($this->container);
+        $u = new User($this->container);
+
+        $this->assertEquals(1, $p->create(array('name' => 'test')));
+        $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
+
+        // We create an event to move the task to the 2nd column
+        $event = array(
+            'project_id' => 1,
+            'task_id' => 1,
+            'column_id' => 3,
+        );
+
+        // Email should be not be sent
+        $this->container['emailClient']->expects($this->never())->method('send');
+
+        // Our event should be executed
+        $this->assertFalse($action->execute(new GenericEvent($event)));
+    }
+
+    public function testMoveColumn()
+    {
+        $action = new Action\TaskEmail($this->container, 1, Task::EVENT_MOVE_COLUMN);
+        $action->setParam('column_id', 2);
+        $action->setParam('user_id', 1);
+        $action->setParam('subject', 'My email subject');
+
+        // We create a task in the first column
+        $tc = new TaskCreation($this->container);
+        $tf = new TaskFinder($this->container);
+        $p = new Project($this->container);
+        $u = new User($this->container);
+
+        $this->assertEquals(1, $p->create(array('name' => 'test')));
+        $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
+        $this->assertTrue($u->update(array('id' => 1, 'email' => 'admin@localhost')));
+
+        // We create an event to move the task to the 2nd column
+        $event = array(
+            'project_id' => 1,
+            'task_id' => 1,
+            'column_id' => 2,
+        );
+
+        // Email should be sent
+        $this->container['emailClient']->expects($this->once())
+            ->method('send')
+            ->with(
+                $this->equalTo('admin@localhost'),
+                $this->equalTo('admin'),
+                $this->equalTo('My email subject'),
+                $this->stringContains('test')
+            );
+
+        // Our event should be executed
+        $this->assertTrue($action->execute(new GenericEvent($event)));
+    }
+
+    public function testTaskClose()
+    {
+        $action = new Action\TaskEmail($this->container, 1, Task::EVENT_CLOSE);
+        $action->setParam('column_id', 2);
+        $action->setParam('user_id', 1);
+        $action->setParam('subject', 'My email subject');
+
+        // We create a task in the first column
+        $tc = new TaskCreation($this->container);
+        $tf = new TaskFinder($this->container);
+        $p = new Project($this->container);
+        $u = new User($this->container);
+
+        $this->assertEquals(1, $p->create(array('name' => 'test')));
+        $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
+        $this->assertTrue($u->update(array('id' => 1, 'email' => 'admin@localhost')));
+
+        // We create an event
+        $event = array(
+            'project_id' => 1,
+            'task_id' => 1,
+            'column_id' => 2,
+        );
+
+        // Email should be sent
+        $this->container['emailClient']->expects($this->once())
+            ->method('send')
+            ->with(
+                $this->equalTo('admin@localhost'),
+                $this->equalTo('admin'),
+                $this->equalTo('My email subject'),
+                $this->stringContains('test')
+            );
+
+        // Our event should be executed
+        $this->assertTrue($action->execute(new GenericEvent($event)));
+    }
+}
\ No newline at end of file
diff --git a/tests/units/ActionTaskUpdateStartDateTest.php b/tests/units/ActionTaskUpdateStartDateTest.php
index 4bf87452..a7df820f 100644
--- a/tests/units/ActionTaskUpdateStartDateTest.php
+++ b/tests/units/ActionTaskUpdateStartDateTest.php
@@ -7,7 +7,6 @@ use Model\Task;
 use Model\TaskCreation;
 use Model\TaskFinder;
 use Model\Project;
-use Integration\GithubWebhook;
 
 class ActionTaskUpdateStartDateTest extends Base
 {
diff --git a/tests/units/Base.php b/tests/units/Base.php
index bc6518fb..d2190c32 100644
--- a/tests/units/Base.php
+++ b/tests/units/Base.php
@@ -11,22 +11,6 @@ use SimpleLogger\File;
 
 date_default_timezone_set('UTC');
 
-class FakeEmailClient
-{
-    public $email;
-    public $name;
-    public $subject;
-    public $html;
-
-    public function send($email, $name, $subject, $html)
-    {
-        $this->email = $email;
-        $this->name = $name;
-        $this->subject = $subject;
-        $this->html = $html;
-    }
-}
-
 class FakeHttpClient
 {
     private $url = '';
@@ -103,7 +87,7 @@ abstract class Base extends PHPUnit_Framework_TestCase
         $this->container['logger'] = new Logger;
         $this->container['logger']->setLogger(new File('/dev/null'));
         $this->container['httpClient'] = new FakeHttpClient;
-        $this->container['emailClient'] = new FakeEmailClient;
+        $this->container['emailClient'] = $this->getMockBuilder('EmailClient')->setMethods(array('send'))->getMock();
     }
 
     public function tearDown()
diff --git a/tests/units/ConfigTest.php b/tests/units/ConfigTest.php
index b630f284..f93619d6 100644
--- a/tests/units/ConfigTest.php
+++ b/tests/units/ConfigTest.php
@@ -17,6 +17,7 @@ class ConfigTest extends Base
         $this->assertEmpty($c->get('webhook_url_task_modification'));
         $this->assertEmpty($c->get('webhook_url_task_creation'));
         $this->assertEmpty($c->get('board_columns'));
+        $this->assertEmpty($c->get('application_url'));
 
         $this->assertNotEmpty($c->get('webhook_token'));
         $this->assertNotEmpty($c->get('api_token'));
@@ -55,4 +56,11 @@ class ConfigTest extends Base
         session_id('');
         unset($this->container['session']);
     }
+
+    public function testSave()
+    {
+        $c = new Config($this->container);
+        $this->assertTrue($c->save(array('application_url' => 'http://localhost/')));
+        $this->assertEquals('http://localhost/', $c->get('application_url'));
+    }
 }
diff --git a/tests/units/NotificationTest.php b/tests/units/NotificationTest.php
index 92b839c4..4ae46ee3 100644
--- a/tests/units/NotificationTest.php
+++ b/tests/units/NotificationTest.php
@@ -285,14 +285,18 @@ class NotificationTest extends Base
         $this->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
         $this->assertTrue($pp->addMember(1, 2));
 
+        $this->container['emailClient']->expects($this->once())
+            ->method('send')
+            ->with(
+                $this->equalTo('user1@here'),
+                $this->equalTo('user1'),
+                $this->equalTo('[test][Task opened] blah (#2)'),
+                $this->stringContains('blah')
+            );
+
         $n->sendNotifications('task.open', array('task' => array(
             'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 0, 'creator_id' => 2
         )));
-
-        $this->assertEquals('user1@here', $this->container['emailClient']->email);
-        $this->assertEquals('user1', $this->container['emailClient']->name);
-        $this->assertEquals('[test][Task opened] blah (#2)', $this->container['emailClient']->subject);
-        $this->assertNotEmpty($this->container['emailClient']->html);
     }
 
     public function testSendNotificationsToAnotherAssignee()
@@ -306,11 +310,11 @@ class NotificationTest extends Base
         $this->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
         $this->assertTrue($pp->addMember(1, 2));
 
+        $this->container['emailClient']->expects($this->never())->method('send');
+
         $n->sendNotifications('task.open', array('task' => array(
             'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 1, 'creator_id' => 1
         )));
-
-        $this->assertEmpty($this->container['emailClient']->email);
     }
 
     public function testSendNotificationsToNotMember()
@@ -323,10 +327,10 @@ class NotificationTest extends Base
         $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
         $this->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
 
+        $this->container['emailClient']->expects($this->never())->method('send');
+
         $n->sendNotifications('task.open', array('task' => array(
             'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 0, 'creator_id' => 2
         )));
-
-        $this->assertEmpty($this->container['emailClient']->email);
     }
 }
diff --git a/tests/units/ProjectTest.php b/tests/units/ProjectTest.php
index fec53c2b..45bc63fc 100644
--- a/tests/units/ProjectTest.php
+++ b/tests/units/ProjectTest.php
@@ -40,7 +40,7 @@ class ProjectTest extends Base
         $this->assertEquals(1, $project['is_active']);
         $this->assertEquals(0, $project['is_public']);
         $this->assertEquals(0, $project['is_private']);
-        $this->assertEquals(time(), $project['last_modified']);
+        $this->assertEquals(time(), $project['last_modified'], '', 1);
         $this->assertEmpty($project['token']);
     }
 
-- 
cgit v1.2.3