Commit 971e16fe authored by Patrick Barroca's avatar Patrick Barroca 🐧

rel #46014 : dedicated group and subscribed group handled in duplication

parent a4646b81
......@@ -51,7 +51,7 @@ class Admin_NewsletterController extends ZendAfi_Controller_Action {
return;
}
if(!$newsletter->duplicate())
if (!$newsletter->duplicate())
$this->_helper->notify($this->_('Duplication impossible: Erreur lors de l\'enregisrement de la copie.'));
}
......
......@@ -338,6 +338,9 @@ class Class_Newsletter extends Storm_Model_Abstract {
public function duplicate() {
if ($this->isNew())
return false;
$attributes = $this->attributesToArray();
unset($attributes['id']);
unset($attributes['last_distribution_date']);
......@@ -349,16 +352,32 @@ class Class_Newsletter extends Storm_Model_Abstract {
if (!$duplicate->save())
return false;
$subscriptions = Class_NewsletterSubscription::findAllBy(['newsletter_id' => (int) $this->getId()]);
foreach ($subscriptions as $subscription)
Class_NewsletterSubscription::newWith($duplicate,
$subscription->getUser());
$this
->duplicateDedicatedGroupIn($duplicate)
->duplicateGroupSubscriptionsIn($duplicate);
return true;
}
protected function duplicateDedicatedGroupIn($other) {
$my_dedicated = $this->getDedicatedGroup();
if (!$ids = $my_dedicated->getUsersIds())
return $this;
$other->getDedicatedGroup()->addAllUsersByIds($ids);
return $this;
}
protected function duplicateGroupSubscriptionsIn($other) {
$other->setUserGroups($this->getUserGroups())
->save();
return $this;
}
public function getLibelle() {
return $this->getTitre();
}
......
......@@ -34,6 +34,27 @@ class UserGroupLoader extends Storm_Model_Loader {
return $ids;
}
public function addAllUsersByIdsTo($ids, $group) {
foreach(array_chunk($ids, 1000) as $chunk) {
$this->_addUsersByIdsTo($chunk, $group);
$this->_cleanMemory();
}
}
protected function _addUsersByIdsTo($ids, $group) {
foreach($ids as $id)
$this->_addUserByIdTo($id, $group);
}
protected function _addUserByIdTo($id, $group) {
Class_UserGroupMembership::newInstance(['user_id' => $id,
'user_group_id' => $group->getId()])
->save();
}
}
......@@ -538,4 +559,10 @@ class Class_UserGroup extends Storm_Model_Abstract {
public function isDedicated() {
return $this->hasModelClass() && $this->hasModelId();
}
public function addAllUsersByIds($ids) {
$this->getLoader()->addAllUsersByIdsTo($ids, $this);
return $this;
}
}
\ No newline at end of file
......@@ -1048,7 +1048,9 @@ class Admin_NewsletterControllerNeverSendTest extends Admin_NewsletterController
class Admin_NewsletterControllerDuplicateActionTest extends Admin_AbstractControllerTestCase {
class Admin_NewsletterControllerDuplicateActionTest
extends Admin_AbstractControllerTestCase {
protected
$_storm_default_to_volatile = true,
$_new_id = null;
......@@ -1056,28 +1058,37 @@ class Admin_NewsletterControllerDuplicateActionTest extends Admin_AbstractContro
public function setUp() {
parent::setUp();
$subscription = $this->fixture('Class_Users', ['id' => 1,
'id_user' => 1,
'nom' => 'Jean',
'prenom' => 'Jean',
'login' => 'jean',
'password' => 'jean']);
$jean = $this->fixture('Class_Users',
['id' => 1,
'nom' => 'Jean',
'prenom' => 'Jean',
'login' => 'jean',
'password' => 'jean']);
$my_group = $this->fixture('Class_UserGroup',
['id' => 14,
'libelle' => 'My Group']);
$newsletter = $this->fixture('Class_Newsletter',
['id' => 1,
'titre' => 'Nouveautés classique',
'contenu' => 'Notre sélection du mois',
'user_groups' => [$my_group]]);
$newsletter = $this->fixture('Class_Newsletter', ['id' => 1,
'titre' => 'Nouveautés classique',
'contenu' => 'Notre sélection du mois']);
$newsletter->getDedicatedGroup()
->addUser($jean)
->save();
Class_Newsletter::find(1)->setLastDistributionDateWithFormat();
$this->fixture('Class_NewsletterSubscription', ['id' => 100,
'newsletter_id' => 1,
'user_id' => 1]);
$newsletter->addSubscription($subscription);
Class_UserGroup::setMemoryCleaner(function() {});
$this->dispatch('/admin/newsletter/duplicate/id/1', true);
if ($new_newsletter = Class_Newsletter::findFirstBy(['titre' => 'Nouveautés classique (copie)']))
$this->_new_id = $new_newsletter->getId();
Class_Newsletter::clearCache();
}
......@@ -1088,26 +1099,36 @@ class Admin_NewsletterControllerDuplicateActionTest extends Admin_AbstractContro
/** @test */
public function duplicateNewsletterShouldExit() {
public function duplicateShouldExist() {
$this->assertNotNull(Class_Newsletter::find($this->_new_id));
}
/** @test */
public function duplicatedNewsletterShouldBeDraft() {
$this->assertEquals(1,Class_Newsletter::find($this->_new_id)->getDraft());
public function duplicateShouldBeDraft() {
$this->assertEquals(1, Class_Newsletter::find($this->_new_id)->getDraft());
}
/** @test */
public function duplicateShouldHaveSameContentThanSource() {
$this->assertEquals(Class_Newsletter::find(1)->getContenu(),
Class_Newsletter::find($this->_new_id)->getContenu());
}
/** @test */
public function duplicatedNewsletterShouldHaveSameContentThanSource() {
$this->assertEquals(Class_Newsletter::find(1)->getContenu(), Class_Newsletter::find($this->_new_id)->getContenu());
public function duplicateShouldHaveJeanInItsDedicatedGroup() {
$this->assertEquals('jean',
Class_Newsletter::find($this->_new_id)
->getDedicatedGroup()
->getUsers()[0]->getLogin());
}
/** @test */
public function duplicatedNewsletterShouldHaveSameSubscriptionThanSource() {
$this->assertNotNull(Class_NewsletterSubscription::findFirstBy(['newsletter_id' => $this->_new_id, 'user_id' => 1]));
public function duplicateShouldHaveMyGroupSubscribed() {
$this->assertTrue(Class_Newsletter::find($this->_new_id)->hasUserGroups());
}
}
......
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