diff options
-rw-r--r-- | app/Action/TaskEmail.php | 97 | ||||
-rw-r--r-- | app/Helper/Url.php | 8 | ||||
-rw-r--r-- | app/Model/Action.php | 4 | ||||
-rw-r--r-- | app/Template/action/index.php | 2 | ||||
-rw-r--r-- | app/Template/action/params.php | 2 | ||||
-rw-r--r-- | docs/automatic-actions.markdown | 1 | ||||
-rw-r--r-- | tests/units.mysql.xml | 2 | ||||
-rw-r--r-- | tests/units.postgres.xml | 2 | ||||
-rw-r--r-- | tests/units.sqlite.xml | 2 | ||||
-rw-r--r-- | tests/units/ActionTaskEmailTest.php | 149 | ||||
-rw-r--r-- | tests/units/ActionTaskUpdateStartDateTest.php | 1 | ||||
-rw-r--r-- | tests/units/Base.php | 18 | ||||
-rw-r--r-- | tests/units/ConfigTest.php | 8 | ||||
-rw-r--r-- | tests/units/NotificationTest.php | 22 | ||||
-rw-r--r-- | tests/units/ProjectTest.php | 2 |
15 files changed, 280 insertions, 40 deletions
diff --git a/app/Action/TaskEmail.php b/app/Action/TaskEmail.php new file mode 100644 index 00000000..363a01c6 --- /dev/null +++ b/app/Action/TaskEmail.php @@ -0,0 +1,97 @@ +<?php + +namespace Action; + +use Model\Task; + +/** + * Email a task to someone + * + * @package action + * @author Frederic Guillot + */ +class TaskEmail extends Base +{ + /** + * Get the list of compatible events + * + * @access public + * @return array + */ + public function getCompatibleEvents() + { + return array( + Task::EVENT_MOVE_COLUMN, + Task::EVENT_CLOSE, + ); + } + + /** + * Get the required parameter for the action (defined by the user) + * + * @access public + * @return array + */ + public function getActionRequiredParameters() + { + return array( + 'column_id' => t('Column'), + 'user_id' => t('User that will receive the email'), + 'subject' => t('Email subject'), + ); + } + + /** + * Get the required parameter for the event + * + * @access public + * @return string[] + */ + public function getEventRequiredParameters() + { + return array( + 'task_id', + 'column_id', + ); + } + + /** + * Execute the action (move the task to another column) + * + * @access public + * @param array $data Event data dictionary + * @return bool True if the action was executed or false when not executed + */ + public function doAction(array $data) + { + $user = $this->user->getById($this->getParam('user_id')); + + if (! empty($user['email'])) { + + $task = $this->taskFinder->getDetails($data['task_id']); + + $this->emailClient->send( + $user['email'], + $user['name'] ?: $user['username'], + $this->getParam('subject'), + $this->template->render('notification/task_create', array('task' => $task, 'application_url' => $this->config->get('application_url'))) + ); + + return true; + } + + return false; + } + + /** + * Check if the event data meet the action condition + * + * @access public + * @param array $data Event data dictionary + * @return bool + */ + public function hasRequiredCondition(array $data) + { + return $data['column_id'] == $this->getParam('column_id'); + } +} diff --git a/app/Helper/Url.php b/app/Helper/Url.php index 64b2c83f..e133f195 100644 --- a/app/Helper/Url.php +++ b/app/Helper/Url.php @@ -88,13 +88,7 @@ class Url extends \Core\Base */ public function base() { - $application_url = $this->config->get('application_url'); - - if (! empty($application_url)) { - return $application_url; - } - - return $this->server(); + return $this->config->get('application_url') ?: $this->server(); } /** diff --git a/app/Model/Action.php b/app/Model/Action.php index c3bfe017..7547e37e 100644 --- a/app/Model/Action.php +++ b/app/Model/Action.php @@ -58,6 +58,7 @@ class Action extends Base 'TaskAssignCategoryLabel' => t('Change the category based on an external label'), 'TaskUpdateStartDate' => t('Automatically update the start date'), 'TaskMoveColumnCategoryChange' => t('Move the task to another column when the category is changed'), + 'TaskEmail' => t('Send a task by email to someone'), ); asort($values); @@ -351,10 +352,13 @@ class Action extends Base $categoryTemplate = $this->category->getById($param['value']); $categoryFromNewProject = $this->db->table(Category::TABLE)->eq('project_id', $project_to)->eq('name', $categoryTemplate['name'])->findOne(); return $categoryFromNewProject['id']; + case 'src_column_id': + case 'dest_column_id': case 'column_id': $boardTemplate = $this->board->getColumn($param['value']); $boardFromNewProject = $this->db->table(Board::TABLE)->eq('project_id', $project_to)->eq('title', $boardTemplate['title'])->findOne(); return $boardFromNewProject['id']; + // TODO: Add user_id default: return $param['value']; } diff --git a/app/Template/action/index.php b/app/Template/action/index.php index ca9c6543..6898fc26 100644 --- a/app/Template/action/index.php +++ b/app/Template/action/index.php @@ -42,7 +42,7 @@ <?= $this->text->in($param['value'], $colors_list) ?> <?php elseif ($this->text->contains($param['name'], 'category_id')): ?> <?= $this->text->in($param['value'], $categories_list) ?> - <?php elseif ($this->text->contains($param['name'], 'label')): ?> + <?php else: ?> <?= $this->e($param['value']) ?> <?php endif ?> </strong> diff --git a/app/Template/action/params.php b/app/Template/action/params.php index 685cbcc5..759c5968 100644 --- a/app/Template/action/params.php +++ b/app/Template/action/params.php @@ -28,7 +28,7 @@ <?php elseif ($this->text->contains($param_name, 'category_id')): ?> <?= $this->form->label($param_desc, $param_name) ?> <?= $this->form->select('params['.$param_name.']', $categories_list, $values) ?><br/> - <?php elseif ($this->text->contains($param_name, 'label')): ?> + <?php else: ?> <?= $this->form->label($param_desc, $param_name) ?> <?= $this->form->text('params['.$param_name.']', $values) ?> <?php endif ?> diff --git a/docs/automatic-actions.markdown b/docs/automatic-actions.markdown index f99212d6..b812f9cc 100644 --- a/docs/automatic-actions.markdown +++ b/docs/automatic-actions.markdown @@ -64,6 +64,7 @@ List of available actions - Change the category based on an external label - Create a comment from an external provider - Automatically update the start date +- Send a task by email to someone Examples -------- diff --git a/tests/units.mysql.xml b/tests/units.mysql.xml index 0e308a05..c6e28fdb 100644 --- a/tests/units.mysql.xml +++ b/tests/units.mysql.xml @@ -1,4 +1,4 @@ -<phpunit> +<phpunit stopOnError="true" stopOnFailure="true" colors="true"> <testsuites> <testsuite name="Kanboard"> <directory>units</directory> diff --git a/tests/units.postgres.xml b/tests/units.postgres.xml index 22e09c03..56cc533e 100644 --- a/tests/units.postgres.xml +++ b/tests/units.postgres.xml @@ -1,4 +1,4 @@ -<phpunit> +<phpunit stopOnError="true" stopOnFailure="true" colors="true"> <testsuites> <testsuite name="Kanboard"> <directory>units</directory> diff --git a/tests/units.sqlite.xml b/tests/units.sqlite.xml index 9771a2a2..9874e169 100644 --- a/tests/units.sqlite.xml +++ b/tests/units.sqlite.xml @@ -1,4 +1,4 @@ -<phpunit> +<phpunit stopOnError="true" stopOnFailure="true" colors="true"> <testsuites> <testsuite name="Kanboard"> <directory>units</directory> 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']); } |