Commit 55cd4152 authored by efalcy's avatar efalcy

dev #46014 : display progress bar for newsletters and continue non ended…

dev #46014 : display progress bar for newsletters and continue non ended dispatch when user resend newsletter
parent a85d4ac3
......@@ -30,12 +30,12 @@ $adapter->query('CREATE TABLE IF NOT EXISTS `newsletter_dispatch` ('
$adapter->query('CREATE TABLE IF NOT EXISTS `newsletter_dispatch_user` ('
. 'id int(11) unsigned not null auto_increment,'
. 'dispath_id int(11) unsigned not null,'
. 'dispatch_id int(11) unsigned not null,'
. 'user_id int(11) not null,'
. 'mail varchar(255) not null,'
. 'sent tinyint(1) not null default 0,'
. 'PRIMARY KEY (`id`),'
. 'KEY `dispath_id` (`dispath_id`),'
. 'KEY `dispatch_id` (`dispatch_id`),'
. 'KEY `user_id` (`user_id`)'
.') engine=MyISAM default charset=utf8');
......
......@@ -99,7 +99,9 @@ class Class_Newsletter extends Storm_Model_Abstract {
public function send() {
$dispatch = Class_Newsletter_Dispatch::newFrom($this);
if (!$dispatch = $this->getLastDispatchInProgress())
$dispatch = $Class_Newsletter_Dispatch::newFrom($this);
return (new Class_Batch_SendNewsletters($dispatch))->run();
}
......@@ -363,6 +365,10 @@ class Class_Newsletter extends Storm_Model_Abstract {
public function getLibelle() {
return $this->getTitre();
}
public function getLastDispatchInProgress() {
return Class_Newsletter_Dispatch::lastDispatchInProgressFor($this);
}
}
......
......@@ -31,6 +31,14 @@ class Newsletter_DispatchLoader extends Storm_Model_Loader {
return $model;
}
public function lastDispatchInProgressFor($newsletter) {
return Class_Newsletter_Dispatch::findFirstBy(['newsletter_id' => $newsletter->getId(),
'order' => 'created_on desc',
'ended_on' => null]);
}
}
......@@ -47,9 +55,14 @@ class Class_Newsletter_Dispatch extends Storm_Model_Abstract {
'role' => 'dispatch',
'dependents' => 'delete'],
'done_users' => ['model' => 'Class_Newsletter_DispatchUser',
'role' => 'dispatch',
'scope' => ['sent' => 1]],
'users' => ['through' => 'dispatch_users']];
protected $_default_attribute_values = ['collected' => 0];
protected $_default_attribute_values = ['collected' => 0,
'ended_on' => null];
public function beforeSave() {
......@@ -72,10 +85,12 @@ class Class_Newsletter_Dispatch extends Storm_Model_Abstract {
public function collectRecipients() {
if ($this->getCollected())
return;
$closure = function($model) {
xdebug_break();
if (!$model->hasMail())
return;
......@@ -110,6 +125,7 @@ class Class_Newsletter_Dispatch extends Storm_Model_Abstract {
public function beEnded() {
$this->setEndedOn($this->getCurrentDateTime())->save();
$this->getNewsletter()->setLastDistributionDateWithFormat();
return $this;
}
}
......@@ -16,30 +16,34 @@
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_View_Helper_getSendProgressJsonFor extends ZendAfi_View_Helper_BaseHelper {
public function getSendProgressJsonFor($newsletter) {
if(!$newsletter)
return $this->toJson($this->view->_('Newsletter inconnue'));
return $this->toJson($this->_('Newsletter inconnue'));
($last_date = DateTime::createFromFormat("Y-m-d H:i:s", $newsletter->getLastDistributionDate()))
($last_date = DateTime::createFromFormat("Y-m-d H:i:s", $newsletter->getLastDistributionDate()))
? $last_date = $this->toJson($last_date->format("d/m/Y H:i"))
: $last_date = $this->toJson($this->view->_('Aucune'));
: $last_date = $this->toJson($this->_('Aucune'));
$progress = Class_NewsletterSubscription::countDistributionProgress($newsletter->getId());
$total=Class_NewsletterSubscription::countTotalDistribution($newsletter->getId());
if($progress < 1)
$dispatch = $newsletter->getLastDispatchInProgress();
if(!$dispatch)
return $last_date;
return json_encode(['status' => $this->view->_('en cours'),
'done' => $total - $progress,
'total' => $total]);
if (!$dispatch->getCollected())
return $this->toJson($this->_('en cours'));
return json_encode(['status' => $this->_('en cours'),
'done' => $dispatch->numberOfDoneUsers(),
'total' => $dispatch->numberOfDispatchUsers()]);
}
protected function toJson($data) {
return json_encode(['status' => $data]);
}
......
......@@ -37,6 +37,8 @@ class ZendAfi_View_Helper_TagProgressBarForNewsletter extends ZendAfi_View_Helpe
Class_ScriptLoader::getInstance()
->addInlineScript($progress_data)
->addJQueryReady('getNewsletterProgress'.$newsletter->getId().'()');
return $this->view->tag('div','',['id' => 'progress_bar_newsletter_'.$newsletter->getId()]);
return $this->_tag('div', '',
['id' => 'progress_bar_newsletter_' . $newsletter->getId()]);
}
}
\ No newline at end of file
......@@ -40,4 +40,4 @@ require_once "startup.php";
setupOpac();
Class_Newsletter_Dispatch::find($argv[4])->generateMails(20);
\ No newline at end of file
Class_Newsletter_Dispatch::find($argv[4])->sendBy(20);
\ No newline at end of file
......@@ -32,6 +32,7 @@ abstract class Admin_NewsletterControllerTestCase
Zend_Mail::setDefaultTransport($this->mock_transport);
Class_Newsletter::setTimeSource(new TimeSourceForTest('2005-03-27 12:30:00'));
Class_UserGroup::setMemoryCleaner(function() {});
$this->onLoaderOfModel('Class_PanierNotice')
->whenCalled('findAllBelongsToAdmin')
......@@ -68,7 +69,8 @@ abstract class Admin_NewsletterControllerTestCase
$pat = $this->fixture('Class_Users',
['id' => 23,
'login' => 'pat',
'password' => 'tap']);
'password' => 'tap',
'mail' => 'pat@pat.info']);
$laurent = $this->fixture('Class_Users',
['id' => 22,
......@@ -76,7 +78,8 @@ abstract class Admin_NewsletterControllerTestCase
'password' => 'all',
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
'idabon' => '0007',
'id_site' => 1]);
'id_site' => 1,
'mail' => 'lla@all.info']);
$my_group = $this->fixture('Class_UserGroup',
['id' => 14,
......@@ -512,6 +515,42 @@ class Admin_NewsletterControllerDeleteActionTest extends Admin_AbstractControlle
}
class Admin_NewsletterControllerSendInProgressActionTest extends Admin_NewsletterControllerTestCase {
protected
$_command,
$_dispatch;
public function setUp() {
parent::setUp();
$this->_command = $this->mock()
->whenCalled('exec')
->answers(true);
Class_Batch_SendNewsletters::setCommand($this->_command);
Class_Newsletter_Dispatch::setTimeSource(new TimeSourceForTest('2016-07-21 11:21:38'));
$dispatch=Class_Newsletter_Dispatch::newFrom(Class_Newsletter::find(2));
$dispatch->assertSave();
$dispatch->collectRecipients();
$this->dispatch('/admin/newsletter/send/id/2', true);
$this->_dispatch = Class_Newsletter::find(2)->getDispatchs()[0];
}
public function tearDown() {
Class_Batch_SendNewsletters::setCommand(null);
parent::tearDown();
}
/** @test */
public function shouldHaveOnlyOneDispatch() {
$this->assertEquals(1, Class_Newsletter::find(2)->numberOfDispatchs());
}
}
class Admin_NewsletterControllerSendActionTest extends Admin_NewsletterControllerTestCase {
......@@ -992,39 +1031,20 @@ class Admin_NewsletterControllerSendProgressWithWrongNewsletterIdTest extends Ad
class Admin_NewsletterControllerSendProgressEndTest extends Admin_AbstractControllerTestCase {
protected $_storm_default_to_volatile = true;
class Admin_NewsletterControllerSendProgressEndTest extends Admin_NewsletterControllerTestCase {
public function setUp() {
parent::setUp();
$dispatch=Class_Newsletter_Dispatch::newFrom(Class_Newsletter::find(1))
->assertSave();
$this->mock_transport = new MockMailTransport();
Zend_Mail::setDefaultTransport($this->mock_transport);
$this->fixture('Class_Newsletter',
['id' => 48,
'titre' => 'News of the Juny',
'contenu' => 'A lot of new stuff',
'last_distribution_date' => '2032-06-27 12:30:00']);
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countDistributionProgress')
->with(48)
->answers(0)
->whenCalled('countTotalDistribution')
->with(48)
->answers(500)
->beStrict();
$this->dispatch('admin/newsletter/send-progress/id/48', true);
$this->dispatch('admin/newsletter/send-progress/id/1', true);
}
/** @test */
public function progressShouldReturnLastDateDistribution() {
$this->assertEquals('{"status":"27\/06\/2032 12:30"}', $this->_response->getBody());
public function progressShouldReturnEnCours() {
$this->assertEquals('{"status":"en cours"}', $this->_response->getBody());
}
}
......@@ -1034,30 +1054,17 @@ class Admin_NewsletterControllerSendProgressTest extends Admin_NewsletterControl
public function setUp() {
parent::setUp();
$this->fixture('Class_Newsletter',
['id' => 3,
'titre' => 'News of the Juny',
'contenu' => 'A lot of new stuff',
'last_distribution_date' => '']);
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countDistributionProgress')
->with(3)
->answers(9300)
$dispatch=Class_Newsletter_Dispatch::newFrom(Class_Newsletter::find(1));
$dispatch->assertSave();
$dispatch->collectRecipients();
->whenCalled('countTotalDistribution')
->with(3)
->answers(9800)
->beStrict();
$this->dispatch('admin/newsletter/send-progress/id/3', true);
$this->dispatch('admin/newsletter/send-progress/id/1', true);
}
/** @test */
public function progressShouldReturnSending() {
$this->assertEquals('{"status":"en cours","done":500,"total":9800}', $this->_response->getBody());
$this->assertEquals('{"status":"en cours","done":0,"total":2}', $this->_response->getBody());
}
}
......@@ -1073,17 +1080,6 @@ class Admin_NewsletterControllerNeverSendTest extends Admin_NewsletterController
'contenu' => 'A lot of new stuff',
'last_distribution_date' => '']);
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countDistributionProgress')
->with(3)
->answers(0)
->whenCalled('countTotalDistribution')
->with(3)
->answers(600)
->beStrict();
$this->dispatch('admin/newsletter/send-progress/id/3', true);
}
......
......@@ -69,6 +69,7 @@ abstract class Class_NewsletterDispatchTestCase extends ModelTestCase {
Class_UserGroup::setMemoryCleaner(function() {});
Class_Newsletter_Dispatch::setTimeSource(new TimeSourceForTest('2016-07-21 15:54:25'));
Class_Newsletter::setTimeSource(new TimeSourceForTest('2016-07-21 15:54:25'));
$this->_dispatch = Class_Newsletter_Dispatch::newFrom($newsletter);
$this->mock_transport = new MockMailTransport();
......@@ -102,6 +103,11 @@ class Class_NewsletterDispatchFirstRunTest extends Class_NewsletterDispatchTestC
$this->assertEquals('2016-07-21 15:54:25', $this->_dispatch->getEndedOn());
}
/** @test */
public function newsletterShouldBeEnded() {
$this->assertEquals('2016-07-21 15:54:25', $this->_dispatch->getNewsletter()->getLastDistributionDate());
}
/** @test */
public function patShouldBeSent() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment