Add automatic action to send a task by email

This commit is contained in:
Frederic Guillot 2015-06-20 10:48:47 -04:00
parent 7056d14c95
commit cb0916d10e
15 changed files with 280 additions and 40 deletions

97
app/Action/TaskEmail.php Normal file
View File

@ -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');
}
}

View File

@ -88,13 +88,7 @@ class Url extends \Core\Base
*/ */
public function base() public function base()
{ {
$application_url = $this->config->get('application_url'); return $this->config->get('application_url') ?: $this->server();
if (! empty($application_url)) {
return $application_url;
}
return $this->server();
} }
/** /**

View File

@ -58,6 +58,7 @@ class Action extends Base
'TaskAssignCategoryLabel' => t('Change the category based on an external label'), 'TaskAssignCategoryLabel' => t('Change the category based on an external label'),
'TaskUpdateStartDate' => t('Automatically update the start date'), 'TaskUpdateStartDate' => t('Automatically update the start date'),
'TaskMoveColumnCategoryChange' => t('Move the task to another column when the category is changed'), 'TaskMoveColumnCategoryChange' => t('Move the task to another column when the category is changed'),
'TaskEmail' => t('Send a task by email to someone'),
); );
asort($values); asort($values);
@ -351,10 +352,13 @@ class Action extends Base
$categoryTemplate = $this->category->getById($param['value']); $categoryTemplate = $this->category->getById($param['value']);
$categoryFromNewProject = $this->db->table(Category::TABLE)->eq('project_id', $project_to)->eq('name', $categoryTemplate['name'])->findOne(); $categoryFromNewProject = $this->db->table(Category::TABLE)->eq('project_id', $project_to)->eq('name', $categoryTemplate['name'])->findOne();
return $categoryFromNewProject['id']; return $categoryFromNewProject['id'];
case 'src_column_id':
case 'dest_column_id':
case 'column_id': case 'column_id':
$boardTemplate = $this->board->getColumn($param['value']); $boardTemplate = $this->board->getColumn($param['value']);
$boardFromNewProject = $this->db->table(Board::TABLE)->eq('project_id', $project_to)->eq('title', $boardTemplate['title'])->findOne(); $boardFromNewProject = $this->db->table(Board::TABLE)->eq('project_id', $project_to)->eq('title', $boardTemplate['title'])->findOne();
return $boardFromNewProject['id']; return $boardFromNewProject['id'];
// TODO: Add user_id
default: default:
return $param['value']; return $param['value'];
} }

View File

@ -42,7 +42,7 @@
<?= $this->text->in($param['value'], $colors_list) ?> <?= $this->text->in($param['value'], $colors_list) ?>
<?php elseif ($this->text->contains($param['name'], 'category_id')): ?> <?php elseif ($this->text->contains($param['name'], 'category_id')): ?>
<?= $this->text->in($param['value'], $categories_list) ?> <?= $this->text->in($param['value'], $categories_list) ?>
<?php elseif ($this->text->contains($param['name'], 'label')): ?> <?php else: ?>
<?= $this->e($param['value']) ?> <?= $this->e($param['value']) ?>
<?php endif ?> <?php endif ?>
</strong> </strong>

View File

@ -28,7 +28,7 @@
<?php elseif ($this->text->contains($param_name, 'category_id')): ?> <?php elseif ($this->text->contains($param_name, 'category_id')): ?>
<?= $this->form->label($param_desc, $param_name) ?> <?= $this->form->label($param_desc, $param_name) ?>
<?= $this->form->select('params['.$param_name.']', $categories_list, $values) ?><br/> <?= $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->label($param_desc, $param_name) ?>
<?= $this->form->text('params['.$param_name.']', $values) ?> <?= $this->form->text('params['.$param_name.']', $values) ?>
<?php endif ?> <?php endif ?>

View File

@ -64,6 +64,7 @@ List of available actions
- Change the category based on an external label - Change the category based on an external label
- Create a comment from an external provider - Create a comment from an external provider
- Automatically update the start date - Automatically update the start date
- Send a task by email to someone
Examples Examples
-------- --------

View File

@ -1,4 +1,4 @@
<phpunit> <phpunit stopOnError="true" stopOnFailure="true" colors="true">
<testsuites> <testsuites>
<testsuite name="Kanboard"> <testsuite name="Kanboard">
<directory>units</directory> <directory>units</directory>

View File

@ -1,4 +1,4 @@
<phpunit> <phpunit stopOnError="true" stopOnFailure="true" colors="true">
<testsuites> <testsuites>
<testsuite name="Kanboard"> <testsuite name="Kanboard">
<directory>units</directory> <directory>units</directory>

View File

@ -1,4 +1,4 @@
<phpunit> <phpunit stopOnError="true" stopOnFailure="true" colors="true">
<testsuites> <testsuites>
<testsuite name="Kanboard"> <testsuite name="Kanboard">
<directory>units</directory> <directory>units</directory>

View File

@ -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)));
}
}

View File

@ -7,7 +7,6 @@ use Model\Task;
use Model\TaskCreation; use Model\TaskCreation;
use Model\TaskFinder; use Model\TaskFinder;
use Model\Project; use Model\Project;
use Integration\GithubWebhook;
class ActionTaskUpdateStartDateTest extends Base class ActionTaskUpdateStartDateTest extends Base
{ {

View File

@ -11,22 +11,6 @@ use SimpleLogger\File;
date_default_timezone_set('UTC'); 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 class FakeHttpClient
{ {
private $url = ''; private $url = '';
@ -103,7 +87,7 @@ abstract class Base extends PHPUnit_Framework_TestCase
$this->container['logger'] = new Logger; $this->container['logger'] = new Logger;
$this->container['logger']->setLogger(new File('/dev/null')); $this->container['logger']->setLogger(new File('/dev/null'));
$this->container['httpClient'] = new FakeHttpClient; $this->container['httpClient'] = new FakeHttpClient;
$this->container['emailClient'] = new FakeEmailClient; $this->container['emailClient'] = $this->getMockBuilder('EmailClient')->setMethods(array('send'))->getMock();
} }
public function tearDown() public function tearDown()

View File

@ -17,6 +17,7 @@ class ConfigTest extends Base
$this->assertEmpty($c->get('webhook_url_task_modification')); $this->assertEmpty($c->get('webhook_url_task_modification'));
$this->assertEmpty($c->get('webhook_url_task_creation')); $this->assertEmpty($c->get('webhook_url_task_creation'));
$this->assertEmpty($c->get('board_columns')); $this->assertEmpty($c->get('board_columns'));
$this->assertEmpty($c->get('application_url'));
$this->assertNotEmpty($c->get('webhook_token')); $this->assertNotEmpty($c->get('webhook_token'));
$this->assertNotEmpty($c->get('api_token')); $this->assertNotEmpty($c->get('api_token'));
@ -55,4 +56,11 @@ class ConfigTest extends Base
session_id(''); session_id('');
unset($this->container['session']); 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'));
}
} }

View File

@ -285,14 +285,18 @@ class NotificationTest extends Base
$this->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1))); $this->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
$this->assertTrue($pp->addMember(1, 2)); $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( $n->sendNotifications('task.open', array('task' => array(
'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 0, 'creator_id' => 2 '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() 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->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
$this->assertTrue($pp->addMember(1, 2)); $this->assertTrue($pp->addMember(1, 2));
$this->container['emailClient']->expects($this->never())->method('send');
$n->sendNotifications('task.open', array('task' => array( $n->sendNotifications('task.open', array('task' => array(
'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 1, 'creator_id' => 1 '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() public function testSendNotificationsToNotMember()
@ -323,10 +327,10 @@ class NotificationTest extends Base
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
$this->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1))); $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( $n->sendNotifications('task.open', array('task' => array(
'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 0, 'creator_id' => 2 'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 0, 'creator_id' => 2
))); )));
$this->assertEmpty($this->container['emailClient']->email);
} }
} }

View File

@ -40,7 +40,7 @@ class ProjectTest extends Base
$this->assertEquals(1, $project['is_active']); $this->assertEquals(1, $project['is_active']);
$this->assertEquals(0, $project['is_public']); $this->assertEquals(0, $project['is_public']);
$this->assertEquals(0, $project['is_private']); $this->assertEquals(0, $project['is_private']);
$this->assertEquals(time(), $project['last_modified']); $this->assertEquals(time(), $project['last_modified'], '', 1);
$this->assertEmpty($project['token']); $this->assertEmpty($project['token']);
} }