Commit ec402152 authored by Patrick Barroca's avatar Patrick Barroca

dev #46014 : fix test failures

parent 2fefa23f
......@@ -21,39 +21,63 @@
class Class_Migration_NewsletterSubscriptionMigration {
use Trait_MemoryCleaner;
protected $_patrons_group;
public function run() {
foreach (Class_Newsletter::findAll() as $newsletter)
$this->_addSubscription($newsletter);
$this->_runOne($newsletter);
}
protected function _addSubscription($newsletter) {
protected function _runOne($newsletter) {
if ($newsletter->getAutoSubscribe())
return $this->_createSigbUserGroup($newsletter) ;
$group = $newsletter->getDedicatedGroup();
$newsletter->addUserGroup($this->_ensurePatronsGroup())->save();
$start_page = 1;
while ($ids = Class_NewsletterSubscription::getAvailableUsersIdsForNewsletter($newsletter->getId(), $start_page)) {
Class_UserGroup::addAllUsersByIdsTo($ids,$group);
while ($existings = Class_NewsletterSubscription::findAllBy(['newsletter_id' => $newsletter->getId(),
'order' => 'id',
'limitPage' => [$start_page, 500]])) {
$this->_runPage($newsletter, $existings);
$this->_cleanMemory();
$start_page++;
}
}
protected function _createSigbUserGroup($newsletter) {
if (! $first_category = Class_UserGroupCategorie::find(1)) {
$first_category = Class_UserGroupCategorie::newInstance([ 'parent_id' => 0,
'libelle' => 'Général']);
$first_category->save();
protected function _ensurePatronsGroup() {
if ($this->_patrons_group)
return $this->_patrons_group;
if (!$category = Class_UserGroupCategorie::find(1)) {
$category = Class_UserGroupCategorie::newInstance(['parent_id' => 0,
'libelle' => 'Général']);
$category->save();
}
$attribs = ['id_cat' => $category->getId(),
'group_type' => Class_UserGroup::TYPE_DYNAMIC,
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
'id_bib' => 0];
if (!$group = Class_UserGroup::findFirstBy($attribs)) {
$group = Class_UserGroup::newInstance(array_merge($attribs, ['libelle' => 'Abonnés']));
$group->save();
}
$group = Class_UserGroup::newInstance(['id_cat' => $first_category->getId(),
'libelle' => 'Abonnés',
'group_type' => Class_UserGroup::TYPE_DYNAMIC,
'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB]);
return $this->_patrons_group = $group;
}
$group->save();
$newsletter->addUserGroup($group)->save();
protected function _runPage($newsletter, $models) {
foreach($models as $model) {
$user = $model->getUser();
if ($newsletter->getAutoSubscribe() && $user->isAbonne())
continue;
$newsletter->subscribeToDedicatedGroup($user);
}
}
}
\ No newline at end of file
......@@ -312,7 +312,7 @@ class Class_Newsletter extends Storm_Model_Abstract {
}
protected function subscribeToDedicatedGroup($user) {
public function subscribeToDedicatedGroup($user) {
$this->getDedicatedGroup()->addUser($user);
$this->save();
return $this;
......
......@@ -18,14 +18,17 @@
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once 'AbstractControllerTestCase.php';
require_once 'tests/fixtures/NewsletterFixtures.php';
abstract class AbstractAbonneControllerNewslettersTestCase extends AbstractControllerTestCase {
protected $_storm_default_to_volatile=true;
protected $_storm_default_to_volatile = true;
public function setUp() {
parent::setUp();
Class_UserGroup::setMemoryCleaner(function() {});
Class_AdminVar::newInstanceWithId('CHAMPS_FICHE_UTILISATEUR',
['valeur' => 'pseudo;nom;prenom;mail;password']);
......@@ -51,6 +54,7 @@ abstract class AbstractAbonneControllerNewslettersTestCase extends AbstractContr
}
abstract class AbonneControllerWithTwoNewslettersTestCase extends AbstractAbonneControllerNewslettersTestCase {
public function setUp() {
parent::setUp();
......@@ -71,6 +75,7 @@ abstract class AbonneControllerWithTwoNewslettersTestCase extends AbstractAbonne
}
class AbonneControllerNewslettersFicheActionWithNoExistingNewslettersTest extends AbstractAbonneControllerNewslettersTestCase {
public function setUp() {
parent::setUp();
......@@ -358,21 +363,18 @@ class AbonneControllerNewslettersSaveWithEmptyDataTest extends AbonneControllerW
public function setUp() {
parent::setUp();
$this->getRequest()->setMethod('POST');
$this->dispatch('/opac/abonne/edit');
$this->postDispatch('/opac/abonne/edit', []);
}
/** @test */
public function marcusShouldHaveBeenSaved() {
$this->assertEquals($this->marcus, Class_Users::findFirstBy(['idabon' => '00123']));
}
public function testNoNewsletters() {
$this->assertEquals(array(), $this->marcus->getNewsletters());
}
public function testPasswordNotChanged() {
/** @test */
public function passwordShouldNotChange() {
$this->assertEquals('mysecret', $this->marcus->getPassword());
}
}
......
......@@ -20,11 +20,25 @@
*/
class Class_Migration_NewsletterSubscriptionSimpleTest extends ModelTestCase {
protected $_storm_default_to_volatile = true;
abstract class Class_Migration_NewsletterSubscriptionTestCase extends ModelTestCase {
protected
$_storm_default_to_volatile = true,
$_migration;
public function setUp() {
parent::setUp();
$this->_migration = (new Class_Migration_NewsletterSubscriptionMigration())
->setMemoryCleaner(function(){});
}
}
class Class_Migration_NewsletterSubscriptionSimpleTest extends Class_Migration_NewsletterSubscriptionTestCase {
public function setUp() {
parent::setUp();
Class_UserGroup::setMemoryCleaner(function() {});
$this->classique = $this->fixture('Class_Newsletter',
['id' => 1,
'titre' => 'Nouveautés classique',
......@@ -42,15 +56,18 @@ class Class_Migration_NewsletterSubscriptionSimpleTest extends ModelTestCase {
'idabon' => '0007',
'id_site' => 1,
'mail' => 'lla@all.info']);
$this->fixture('Class_NewsletterSubscription',
['id' => 2,
'user' => $this->laurent,
'newsletter' => $this->classique]
);
(new Class_Migration_NewsletterSubscriptionMigration())->run();
$this->_migration->run();
}
/** @test */
public function laurentShouldBeSubscribed() {
$this->assertTrue($this->classique->getDedicatedGroup()->hasUser($this->laurent));
......@@ -59,11 +76,10 @@ class Class_Migration_NewsletterSubscriptionSimpleTest extends ModelTestCase {
class Class_Migration_NewsletterSubscriptionWithGuestTest extends ModelTestCase {
protected $_storm_default_to_volatile = true;
class Class_Migration_NewsletterSubscriptionWithGuestTest extends Class_Migration_NewsletterSubscriptionTestCase {
public function setUp() {
parent::setUp();
Class_UserGroup::setMemoryCleaner(function() {});
$this->classique = $this->fixture('Class_Newsletter',
['id' => 1,
'titre' => 'Nouveautés classique',
......@@ -81,6 +97,7 @@ class Class_Migration_NewsletterSubscriptionWithGuestTest extends ModelTestCase
'idabon' => '0007',
'id_site' => 1,
'mail' => 'lla@all.info']);
$this->anonymous = $this->fixture('Class_Users',
['id' => 66,
'login' => 'anonymous',
......@@ -93,15 +110,17 @@ class Class_Migration_NewsletterSubscriptionWithGuestTest extends ModelTestCase
'user' => $this->laurent,
'newsletter' => $this->classique]
);
$this->fixture('Class_NewsletterSubscription',
['id' => 3,
'user' => $this->anonymous,
'newsletter' => $this->classique]
);
(new Class_Migration_NewsletterSubscriptionMigration())->run();
$this->_migration->run();
}
/** @test */
public function sigbuserShouldNotBeInManualGroup() {
$this->assertFalse($this->classique->getDedicatedGroup()->hasUser($this->laurent));
......@@ -110,13 +129,18 @@ class Class_Migration_NewsletterSubscriptionWithGuestTest extends ModelTestCase
/** @test */
public function dynamicGroupShouldBeCreatedForSigbUsers() {
$this->assertNotEmpty($this->classique->getUserGroups());
$groups = $this->classique->getUserGroups();
$this->assertNotEmpty($groups);
$this->assertTrue($groups[0]->isDynamic());
$this->assertEquals(ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB, $groups[0]->getRoleLevel());
$this->assertEquals(0, $groups[0]->getIdBib());
}
/** @test */
public function guestUserShouldBeInManualGroup() {
$this->assertTrue($this->classique->getDedicatedGroup()->hasUser($this->anonymous));
}
}
?>
\ No newline at end of file
}
\ No newline at end of file
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