/** * Test clone, toString */ public function testCloneAndToString() { $translation = $this->getMock('Oro\\Bundle\\EmailBundle\\Entity\\EmailTemplateTranslation'); $this->emailTemplate->getTranslations()->add($translation); $clone = clone $this->emailTemplate; $this->assertNull($clone->getId()); $this->assertEquals($clone->getParent(), $this->emailTemplate->getId()); $this->assertFalse($clone->getIsSystem()); $this->assertTrue($clone->getIsEditable()); $this->assertEquals($this->emailTemplate->getName(), (string) $this->emailTemplate); $this->assertFalse($clone->getTranslations()->first() === $translation); }
/** * @param EmailTemplate $value * @param Constraint|EmailTemplateSyntax $constraint */ public function validate($value, Constraint $constraint) { // prepare templates to be validated $itemsToValidate = [['field' => 'subject', 'locale' => null, 'template' => $value->getSubject()], ['field' => 'content', 'locale' => null, 'template' => $value->getContent()]]; $translations = $value->getTranslations(); foreach ($translations as $trans) { if (in_array($trans->getField(), ['subject', 'content'])) { $itemsToValidate[] = ['field' => $trans->getField(), 'locale' => $trans->getLocale(), 'template' => $trans->getContent()]; } } /** @var \Twig_Extension_Sandbox $sandbox */ $sandbox = $this->twig->getExtension('sandbox'); $sandbox->enableSandbox(); // validate templates' syntax $errors = []; foreach ($itemsToValidate as &$item) { try { $this->twig->parse($this->twig->tokenize($item['template'])); } catch (\Twig_Error_Syntax $e) { $errors[] = ['field' => $item['field'], 'locale' => $item['locale'], 'error' => $e->getMessage()]; } } $sandbox->disableSandbox(); // add violations for found errors if (!empty($errors)) { foreach ($errors as $error) { $this->context->addViolation($constraint->message, ['{{ field }}' => $this->getFieldLabel(ClassUtils::getClass($value), $error['field']), '{{ locale }}' => $this->getLocaleName($error['locale']), '{{ error }}' => $error['error']]); } } }
public function testValidateSandboxErrors() { $trans = new EmailTemplateTranslation(); $trans->setField('subject')->setLocale('fr')->setContent(self::TEST_TRANS_SUBJECT); $this->template->getTranslations()->add($trans); $subjectTokenStream = $this->getMockBuilder('\\Twig_TokenStream')->disableOriginalConstructor()->getMock(); $contentTokenStream = $this->getMockBuilder('\\Twig_TokenStream')->disableOriginalConstructor()->getMock(); $transSubjectTokenStream = $this->getMockBuilder('\\Twig_TokenStream')->disableOriginalConstructor()->getMock(); $tokenizeMap = [[self::TEST_SUBJECT, null, $subjectTokenStream], [self::TEST_CONTENT, null, $contentTokenStream], [self::TEST_TRANS_SUBJECT, null, $transSubjectTokenStream]]; $this->twig->expects($this->exactly(count($tokenizeMap)))->method('tokenize')->will($this->returnValueMap($tokenizeMap)); $this->twig->expects($this->exactly(count($tokenizeMap)))->method('parse')->will($this->throwException(new \Twig_Error_Syntax('message'))); $this->entityConfigProvider->expects($this->exactly(3))->method('hasConfig')->will($this->returnValueMap([[get_class($this->template), 'subject', true], [get_class($this->template), 'content', false]])); $subjectConfig = new Config(new FieldConfigId('entity', get_class($this->template), 'subject', 'string')); $subjectConfig->set('label', 'subject.label'); $this->entityConfigProvider->expects($this->exactly(2))->method('getConfig')->with(get_class($this->template), 'subject')->will($this->returnValue($subjectConfig)); $this->localeSettings->expects($this->exactly(3))->method('getLanguage')->will($this->returnValue('en')); $this->localeSettings->expects($this->exactly(3))->method('getLocalesByCodes')->will($this->returnValueMap([[['en'], 'en', ['en' => 'English']], [['fr'], 'en', ['fr' => 'French']]])); $this->context->expects($this->at(0))->method('addViolation')->with($this->constraint->message, ['{{ field }}' => 'subject.label', '{{ locale }}' => 'English', '{{ error }}' => 'message']); $this->context->expects($this->at(1))->method('addViolation')->with($this->constraint->message, ['{{ field }}' => 'content', '{{ locale }}' => 'English', '{{ error }}' => 'message']); $this->context->expects($this->at(2))->method('addViolation')->with($this->constraint->message, ['{{ field }}' => 'subject.label', '{{ locale }}' => 'French', '{{ error }}' => 'message']); $validator = $this->getValidator(self::ENTITY_CLASS); $validator->validate($this->template, $this->constraint); }
/** * Compile preview content * * @param EmailTemplate $entity * @param null|string $locale * * @return string */ public function compilePreview(EmailTemplate $entity, $locale = null) { $content = $entity->getContent(); if ($locale) { foreach ($entity->getTranslations() as $translation) { /** @var EmailTemplateTranslation $translation */ if ($translation->getLocale() === $locale && $translation->getField() === 'content') { $content = $translation->getContent(); } } } return $this->render('{% verbatim %}' . $content . '{% endverbatim %}', []); }
/** * @param EmailModel $emailModel * @param EmailTemplate $template * @param Email $email */ protected function applyTemplate(EmailModel $emailModel, EmailTemplate $template, Email $email) { $locales = array_merge($email->getAcceptedLocales(), [$this->defaultLocale]); $flippedLocales = array_flip($locales); $translatedSubjects = []; $translatedContents = []; foreach ($template->getTranslations() as $translation) { switch ($translation->getField()) { case 'content': $translatedContents[$translation->getLocale()] = $translation->getContent(); break; case 'subject': $translatedSubjects[$translation->getLocale()] = $translation->getContent(); break; } } $comparator = ArrayUtil::createOrderedComparator($flippedLocales); uksort($translatedSubjects, $comparator); uksort($translatedContents, $comparator); $validContents = array_intersect_key($translatedContents, $flippedLocales); $validsubjects = array_intersect_key($translatedSubjects, $flippedLocales); $content = reset($validContents); $subject = reset($validsubjects); $emailModel->setSubject($subject === false ? $template->getSubject() : $subject)->setBody($content === false ? $template->getContent() : $content)->setType($template->getType()); }