Skip to content

Commit b1606bc

Browse files
committed
update
1 parent e3207a6 commit b1606bc

File tree

2 files changed

+63
-29
lines changed

2 files changed

+63
-29
lines changed

app/code/core/Mage/Newsletter/Model/Template.php

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
1515
*/
1616

17+
use Symfony\Component\Validator\Constraints as Assert;
18+
use Symfony\Component\Validator\Validation;
19+
1720
/**
1821
* Template model
1922
*
@@ -79,38 +82,59 @@ protected function _construct()
7982
*/
8083
public function validate()
8184
{
82-
$validators = [
83-
'template_code' => [Zend_Filter_Input::ALLOW_EMPTY => false],
84-
'template_type' => 'Int',
85-
'template_sender_email' => 'EmailAddress',
86-
'template_sender_name' => [Zend_Filter_Input::ALLOW_EMPTY => false],
87-
];
88-
$data = [];
89-
foreach (array_keys($validators) as $validateField) {
90-
$data[$validateField] = $this->getDataUsingMethod($validateField);
91-
}
92-
93-
$validateInput = new Zend_Filter_Input([], $validators, $data);
94-
if (!$validateInput->isValid()) {
95-
$errorMessages = [];
96-
foreach ($validateInput->getMessages() as $messages) {
97-
if (is_array($messages)) {
98-
foreach ($messages as $message) {
99-
$errorMessages[] = $message;
100-
}
101-
} else {
102-
$errorMessages[] = $messages;
103-
}
85+
$validator = Validation::createValidator();
86+
$violations = [];
87+
$errors = new ArrayObject();
88+
89+
$violations[] = $validator->validate($this->getDataUsingMethod('template_code'), [
90+
new Assert\NotBlank([
91+
'message' => 'You must give a non-empty value for field \'template_code\'',
92+
]),
93+
]);
94+
95+
$message = 'You must give a non-empty value for field \'template_type\'';
96+
$violations[] = $validator->validate($this->getDataUsingMethod('template_type'), [
97+
new Assert\NotBlank([
98+
'message' => $message,
99+
]),
100+
new Assert\Type([
101+
'type' => 'int',
102+
'message' => $message,
103+
]),
104+
]);
105+
106+
$message = '\'invalid-email\' is not a valid email address in the basic format local-part@hostname';
107+
$violations[] = $validator->validate($this->getDataUsingMethod('template_sender_email'), [
108+
new Assert\NotBlank([
109+
'message' => $message,
110+
]),
111+
new Assert\Email([
112+
'message' => $message,
113+
]),
114+
]);
115+
116+
$violations[] = $validator->validate($this->getDataUsingMethod('template_sender_name'), [
117+
new Assert\NotBlank([
118+
'message' => 'You must give a non-empty value for field \'template_sender_name\'',
119+
]),
120+
]);
121+
122+
foreach ($violations as $violation) {
123+
foreach ($violation as $error) {
124+
$errors->append($error->getMessage());
104125
}
126+
}
105127

106-
Mage::throwException(implode("\n", $errorMessages));
128+
if (count($errors) !== 0) {
129+
Mage::throwException(implode("\n", iterator_to_array($errors)));
107130
}
108131
}
109132

110133
/**
111134
* Processing object before save data
112135
*
113136
* @inheritDoc
137+
* @throws Mage_Core_Exception
114138
*/
115139
protected function _beforeSave()
116140
{
@@ -147,7 +171,7 @@ public function isValidForSend()
147171
/**
148172
* Getter for template type
149173
*
150-
* @return int|string
174+
* @return int
151175
*/
152176
public function getType()
153177
{
@@ -280,7 +304,8 @@ public function getMail()
280304
* @param array $variables template variables
281305
* @param string|null $name receiver name (if subscriber model not specified)
282306
* @param Mage_Newsletter_Model_Queue|null $queue queue model, used for problems reporting.
283-
* @return bool
307+
* @return bool
308+
* @throws Exception|Throwable
284309
* @deprecated since 1.4.0.1
285310
**/
286311
public function send($subscriber, array $variables = [], $name = null, ?Mage_Newsletter_Model_Queue $queue = null)
@@ -346,23 +371,23 @@ public function send($subscriber, array $variables = [], $name = null, ?Mage_New
346371
if (!is_null($queue)) {
347372
$subscriber->received($queue);
348373
}
349-
} catch (Exception $e) {
374+
} catch (Exception $exception) {
350375
if ($subscriber instanceof Mage_Newsletter_Model_Subscriber) {
351376
// If letter sent for subscriber, we create a problem report entry
352377
$problem = Mage::getModel('newsletter/problem');
353378
$problem->addSubscriberData($subscriber);
354379
if (!is_null($queue)) {
355380
$problem->addQueueData($queue);
356381
}
357-
$problem->addErrorData($e);
382+
$problem->addErrorData($exception);
358383
$problem->save();
359384

360385
if (!is_null($queue)) {
361386
$subscriber->received($queue);
362387
}
363388
} else {
364389
// Otherwise throw error to upper level
365-
throw $e;
390+
throw $exception;
366391
}
367392
return false;
368393
}
@@ -374,6 +399,7 @@ public function send($subscriber, array $variables = [], $name = null, ?Mage_New
374399
* Prepare Process (with save)
375400
*
376401
* @return $this
402+
* @throws Throwable
377403
* @deprecated since 1.4.0.1
378404
*/
379405
public function preprocess()
@@ -388,6 +414,7 @@ public function preprocess()
388414
* Retrieve processed template subject
389415
*
390416
* @return string
417+
* @throws Exception
391418
*/
392419
public function getProcessedTemplateSubject(array $variables)
393420
{

tests/unit/Mage/Newsletter/Model/TemplateTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function validateTemplateDataProvider(): Generator
6464
'setTemplateSenderName' => 'Sender Name',
6565
'setTemplateSubject' => 'Valid Subject',
6666
'setTemplateText' => 'Valid Template Text',
67-
'setTemplateType' => '1',
67+
'setTemplateType' => 1,
6868
];
6969

7070
yield 'valid data' => [
@@ -79,6 +79,13 @@ public function validateTemplateDataProvider(): Generator
7979
$data,
8080
];
8181

82+
$data = $validData;
83+
$data['setTemplateSenderEmail'] = null;
84+
yield 'missing sender email' => [
85+
'\'invalid-email\' is not a valid email address in the basic format local-part@hostname',
86+
$data,
87+
];
88+
8289
$data = $validData;
8390
$data['setTemplateSenderEmail'] = 'invalid-email';
8491
yield 'invalid sender email' => [

0 commit comments

Comments
 (0)