4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Core\tests\units
;
24 use Galette\GaletteTestCase
;
27 * Mailing history tests class
29 * @author Johan Cwiklinski <johan@x-tnd.be>
31 class MailingHistory
extends GaletteTestCase
33 protected int $seed = 20240131082138;
36 * Cleanup after each test method
40 public function tearDown(): void
44 $this->zdb
= new \Galette\Core\
Db();
45 $delete = $this->zdb
->delete(\Galette\Core\MailingHistory
::TABLE
);
46 $this->zdb
->execute($delete);
50 * Test history workflow
54 public function testHistoryFlow(): void
56 $this->logSuperAdmin();
57 $mh = new \Galette\Core\
MailingHistory(
63 //nothing in the logs at the beginning
64 $list = $mh->getHistory();
65 $this->assertCount(0, $list);
67 $filters = new \Galette\Filters\
MembersList();
68 $adh1 = $this->getMemberOne();
69 $adh2 = $this->getMemberTwo();
70 $filters->selected
= [$adh1->id
, $adh2->id
];
72 $m = new \Galette\Repository\
Members();
73 $members = $m->getArrayList($filters->selected
);
74 $mailing = new \Galette\Core\
Mailing($this->preferences
, $members);
76 $mailing->subject
= 'Test mailing';
77 $mailing->body
= 'This is a test mailing';
79 'name' => 'Galette unit tests',
80 'email' => 'test@galette.eu'
82 $mailing->current_step
= \Galette\Core\Mailing
::STEP_SEND
;
84 $mh = new \Galette\Core\
MailingHistory(
91 //user store mailing request (not send yet)
92 $this->assertTrue($mh->storeMailing());
94 //one entry in the logs
95 $list = $mh->getHistory();
96 $this->assertCount(1, $list);
99 $this->assertSame('Test mailing', $entry->mailing_subject
);
100 $this->assertCount(2, $entry->mailing_recipients
);
101 $this->assertEquals(0, $entry->mailing_sent
);
102 $this->assertSame(0, $entry->attachments
);
104 $first_not_sent_id = $entry->mailing_id
;
106 $mailing = new \Galette\Core\
Mailing($this->preferences
, $members);
107 $mh = new \Galette\Core\
MailingHistory(
114 $this->assertTrue($mh::loadFrom($this->zdb
, $first_not_sent_id, $mailing, false));
116 $this->assertSame('Test mailing', $mailing->subject
);
117 $this->assertCount(2, $entry->mailing_recipients
);
118 $this->assertEquals(0, $entry->mailing_sent
);
119 $this->assertSame(0, $entry->attachments
);
121 //change and store again (still not send yet)
122 $mailing->subject
= 'Test mailing (changed)';
123 $this->assertTrue($mh->storeMailing());
125 //still one entry in the logs
126 $list = $mh->getHistory();
127 $this->assertCount(1, $list);
130 $second_not_sent_id = $entry->mailing_id
;
131 $this->assertSame('Test mailing (changed)', $entry->mailing_subject
);
132 $this->assertCount(2, $entry->mailing_recipients
);
133 $this->assertEquals(0, $entry->mailing_sent
);
134 $this->assertSame(0, $entry->attachments
);
135 $this->assertSame($first_not_sent_id, $second_not_sent_id);
137 $mailing = new \Galette\Core\
Mailing($this->preferences
, $members);
138 $mh = new \Galette\Core\
MailingHistory(
145 $this->assertTrue($mh::loadFrom($this->zdb
, $second_not_sent_id, $mailing, false));
147 //store "sent" mailing
148 $this->assertTrue($mh->storeMailing(true));
150 //still one entry in the logs
151 $list = $mh->getHistory();
152 $this->assertCount(1, $list);
155 $this->assertSame('Test mailing (changed)', $entry->mailing_subject
);
156 $this->assertCount(2, $entry->mailing_recipients
);
157 $this->assertEquals(1, $entry->mailing_sent
);
158 $this->assertSame(0, $entry->attachments
);
159 $this->assertSame($second_not_sent_id, $entry->mailing_id
);
161 //add antoher mailing in history
162 $mailing = new \Galette\Core\
Mailing($this->preferences
, $members);
164 $mailing->subject
= 'Filter subject test';
165 $mailing->body
= 'This is a test mailing for filters';
167 'name' => 'Galette admin unit tests',
168 'email' => 'test+admin@galette.eu'
170 $mailing->current_step
= \Galette\Core\Mailing
::STEP_SEND
;
172 $filters = new \Galette\Filters\
MailingsList();
173 $mh = new \Galette\Core\
MailingHistory(
180 //user store mailing request (not send yet)
181 $this->assertTrue($mh->storeMailing());
183 //still one entry in the logs
184 $list = $mh->getHistory();
185 $this->assertCount(2, $list);
187 $filters->subject_filter
= 'filter';
188 $list = $mh->getHistory();
189 $this->assertCount(1, $list);
191 $filters->subject_filter
= null;
192 $filters->sent_filter
= \Galette\Core\MailingHistory
::FILTER_SENT
;
193 $list = $mh->getHistory();
194 $this->assertCount(1, $list);
197 $this->assertSame('Test mailing (changed)', $entry->mailing_subject
);
198 $this->assertCount(2, $entry->mailing_recipients
);
199 $this->assertEquals(1, $entry->mailing_sent
);