Commit d8d3ef3b authored by efalcy's avatar efalcy

dev #46014 : allow unsubscription for users

parent 05f18906
......@@ -40,6 +40,17 @@ $adapter->query('CREATE TABLE IF NOT EXISTS `newsletter_dispatch_user` ('
.') engine=MyISAM default charset=utf8');
$adapter->query('CREATE TABLE IF NOT EXISTS `newsletter_blacklist` ('
. 'id int(11) unsigned not null auto_increment,'
. 'newsletter_id int(11) unsigned not null,'
. 'user_id int(11) not null,'
. 'mail varchar(255) not null,'
. 'PRIMARY KEY (`id`),'
. 'KEY `mail` (`mail`)'
.') engine=MyISAM default charset=utf8');
try {
$adapter->query('ALTER TABLE user_groups '
. 'ADD COLUMN model_class varchar(255) null default \'\', '
......
......@@ -294,11 +294,8 @@ class Class_Newsletter extends Storm_Model_Abstract {
public function unsubscribeByMail($mail) {
if (!$mail)
return;
$user_ids = [];
foreach(Class_Users::findAllBy(['mail' => $mail]) as $user)
$user_ids[] = $user->getId();
return false;
return Class_Newsletter_Blacklist::unsubscribeByMail($mail,$this->getId());
}
......
<?php
/**
* Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
*
* AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* AFI-OPAC 2.0 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with AFI-OPAC 2.0; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Newsletter_BlacklistLoader extends Storm_Model_Loader {
public function getMailsForNewsletter($newsletter_id) {
$mails=[];
foreach(Class_Newsletter_Blacklist::findAllBy(['newsletter_id' => [$newsletter_id, -1]]) as $blacklist)
$mails[]=$blacklist->getMail();
return $mails;
}
public function unsubscribeByMail($mail,$newsletter_id=-1) {
$blacklist = Class_Newsletter_Blacklist::newInstance(['mail' => $mail,
'newsletter_id' => $newsletter_id]);
return $blacklist->save();
}
public function removeMailForNewsletter($mail,$newsletter_id=-1) {
if ($blacklist = Class_Newsletter_Blacklist::findFirstBy(['mail'=> $mail,
'newsletter_id' => $newsletter_id]))
return $blacklist->delete();
if ($newsletter_id<0)
$blacklist->deleteBy(['mail'=> $mail]);
}
}
class Class_Newsletter_Blacklist extends Storm_Model_Abstract {
use Trait_TimeSource;
protected $_table_name = 'newsletter_blacklist';
protected $_loader_class = 'Newsletter_BlacklistLoader';
protected $_belongs_to = ['newsletter' => ['model' => 'Class_Newsletter']];
protected $_default_attribute_values = ['collected' => 0,
'ended_on' => null];
}
......@@ -88,9 +88,8 @@ class Class_Newsletter_Dispatch extends Storm_Model_Abstract {
if ($this->getCollected())
return;
$closure = function($model) {
xdebug_break();
$blacklist_mails = Class_Newsletter_Blacklist::getMailsForNewsletter($this->getNewsletter()->getId());
$closure = function($model) use ($blacklist_mails) {
if (!$model->hasMail())
return;
......@@ -99,7 +98,8 @@ class Class_Newsletter_Dispatch extends Storm_Model_Abstract {
if (Class_Newsletter_DispatchUser::findFirstBy($params))
return;
if (in_array( $model->getMail(),$blacklist_mails))
return;
Class_Newsletter_DispatchUser::newInstance(['dispatch_id' => $this->getId(),
'user_id' => $model->getId(),
'mail' => $model->getMail()])
......
......@@ -55,6 +55,16 @@ abstract class Admin_NewsletterControllerTestCase
'id_site' => 1,
'mail' => 'lla@all.info']);
$anonymous = $this->fixture('Class_Users',
['id' => 66,
'login' => 'anonymous',
'password' => 'leavemealone',
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
'idabon' => '0009',
'id_site' => 1,
'mail' => 'anonymous@all.info']);
$my_group = $this->fixture('Class_UserGroup',
['id' => 14,
'libelle' => 'My Group',
......@@ -196,7 +206,6 @@ class Admin_NewsletterControllerIndexActionTest extends Admin_NewsletterControll
$this->assertXPathContentContains("//a[@href='/admin/newsletter/edit-subscribers/id/2']", '00002');
}
public function testListAnimationsEditLink() {
$this->assertXPath("//a[@href='/admin/newsletter/edit/id/2']");
}
......@@ -334,17 +343,6 @@ class Admin_NewsletterControllerEditActionTest extends Admin_NewsletterControlle
Class_Newsletter::find(53)->setLastDistributionDateWithFormat();
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countDistributionProgress')
->with(53)
->answers(2)
->whenCalled('countTotalDistribution')
->with(53)
->answers(2)
->whenCalled('countAvailableUserForNewsletter')
->with(53)
->answers(2);
$this->dispatch('/admin/newsletter/edit/id/53', true);
}
......@@ -637,41 +635,6 @@ class Admin_NewsletterControllerPreviewActionTest extends Admin_NewsletterContro
'id_panier' => null,
'last_distribution_date' => '2012-03-02']);
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countAvailableUserForNewsletter')
->with(3)
->answers(2)
->whenCalled('countAvailableUserForNewsletter')
->with(1)
->answers(0)
->whenCalled('countAvailableUserForNewsletter')
->with(2)
->answers(0)
->whenCalled('getAvailableUsersForNewsletter')
->with(3, 0, 200)
->answers([$marcus, $miles])
->whenCalled('getAvailableUsersForNewsletter')
->with(3)
->answers([$marcus, $miles])
->whenCalled('getAvailableUsersForNewsletter')
->with(3,0)
->answers([$marcus, $miles])
->whenCalled('clearCache')
->answers(true)
->whenCalled('countDistributionProgress')
->with(3)
->answers(0)
->whenCalled('countDistributionProgress')
->with(2)
->answers(0)
->whenCalled('countDistributionProgress')
->with(1)
->answers(0)
->beStrict();
$profil_portail = $this->fixture('Class_Profil', ['id' => 1,
'libelle' => 'Portail',
......@@ -790,10 +753,6 @@ class Admin_NewsletterControllerPostSendTestActionTest extends Admin_AbstractCon
'id_catalogue' => 0,
'contenu' => 'mail']);
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countAvailableUserForNewsletter')
->with(4)
->answers(0);
$this->mock_transport = new MockMailTransport();
Zend_Mail::setDefaultTransport($this->mock_transport);
......@@ -928,21 +887,6 @@ class Admin_NewsletterControllerScriptTest extends Admin_NewsletterControllerTes
Class_Newsletter::find(5)->setLastDistributionDateWithFormat();
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countDistributionProgress')
->with(5)
->answers(2)
->whenCalled('countTotalDistribution')
->with(5)
->answers(2)
->whenCalled('countAvailableUserForNewsletter')
->with(5)
->answers(2);
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countDistributionProgress')
->with(5)
->answers(2);
$this->fixture('Class_Newsletter',
['id' => 6,
......@@ -952,24 +896,6 @@ class Admin_NewsletterControllerScriptTest extends Admin_NewsletterControllerTes
Class_Newsletter::find(6)->setLastDistributionDateWithFormat();
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countDistributionProgress')
->with(6)
->answers(2)
->whenCalled('countTotalDistribution')
->with(6)
->answers(2)
->whenCalled('countAvailableUserForNewsletter')
->with(6)
->answers(2);
$this->onLoaderOfModel('Class_NewsletterSubscription')
->whenCalled('countDistributionProgress')
->with(6)
->answers(2);
$this->dispatch('admin/newsletter/index', true);
}
......
......@@ -143,21 +143,7 @@ class NewsletterControllerValidUnsubscribeConfirmTest extends NewsletterControll
/** @test */
public function harlockShouldBeUnsubscribed() {
$this->assertNull(Class_NewsletterSubscription::findFirstBy(['newsletter_id' => 34,
'user_id' => 23]));
$this->assertEquals(['c.harlock@arcadia.net'],Class_Newsletter_Blacklist::getMailForNewsletter(34));
}
/** @test */
public function juniorShouldBeUnsubscribed() {
$this->assertNull(Class_NewsletterSubscription::findFirstBy(['newsletter_id' => 34,
'user_id' => 24]));
}
/** @test */
public function procyonShouldStillBeSubscribed() {
$this->assertNotNull(Class_NewsletterSubscription::findFirstBy(['newsletter_id' => 34,
'user_id' => 25]));
}
}
\ No newline at end of file
......@@ -871,7 +871,8 @@ class UpgradeDB_304_Test extends UpgradeDBTestCase {
->dropIndexedFieldFrom('user_groups', 'model_class')
->dropIndexedFieldFrom('user_groups', 'model_id')
->dropTable('newsletter_dispatch')
->dropTable('newsletter_dispatch_user');
->dropTable('newsletter_dispatch_user')
->dropTable('newsletter_blacklist');
}
......@@ -915,4 +916,10 @@ class UpgradeDB_304_Test extends UpgradeDBTestCase {
public function tableNewsletterDispatchUserShouldExists() {
$this->assertTable('newsletter_dispatch_user');
}
/** @test */
public function tableNewsletterBlackListShouldExists() {
$this->assertTable('newsletter_dispatch_user');
}
}
\ No newline at end of file
......@@ -89,6 +89,14 @@ abstract class NewsletterMailingTestCase extends ModelTestCase {
'mail'=> '',
'login' => 'pommo',
'password' => 'reo']);
$anonymous = $this->fixture('Class_Users',
['id' => 66,
'login' => 'anonymous',
'password' => 'leavemealone',
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
'idabon' => '0009',
'id_site' => 1,
'mail' => 'anonymous@all.info']);
$this->animations = $this->fixture('Class_Newsletter',
['id' => 1,
......@@ -105,9 +113,10 @@ abstract class NewsletterMailingTestCase extends ModelTestCase {
->setUsers([$this->rdubois,
$this->mduchamp,
$this->zork,
$this->zorkglub])
$this->zorkglub,
$anonymous])
->save();
Class_Newsletter_Blacklist::unsubscribeByMail($anonymous->getMail(),1);
$this->onLoaderOfModel('Class_Notice')
->whenCalled('getNoticesFromPreferences')
......
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