public function testToOptionArray() { $collection = $this->getMock('Magento\\Email\\Model\\Resource\\Template\\Collection', [], [], '', false); $collection->expects($this->once())->method('toOptionArray')->will($this->returnValue([['value' => 'template_one', 'label' => 'Template One'], ['value' => 'template_two', 'label' => 'Template Two']])); $this->_coreRegistry->expects($this->once())->method('registry')->with('config_system_email_template')->will($this->returnValue($collection)); $this->_emailConfig->expects($this->once())->method('getTemplateLabel')->with('template_new')->will($this->returnValue('Template New')); $expectedResult = [['value' => 'template_new', 'label' => 'Template New (Default)'], ['value' => 'template_one', 'label' => 'Template One'], ['value' => 'template_two', 'label' => 'Template Two']]; $this->_model->setPath('template/new'); $this->assertEquals($expectedResult, $this->_model->toOptionArray()); }
/** * Generate list of email templates * * @return array */ public function toOptionArray() { /** @var $collection \Magento\Email\Model\Resource\Template\Collection */ if (!($collection = $this->_coreRegistry->registry('config_system_email_template'))) { $collection = $this->_templatesFactory->create(); $collection->load(); $this->_coreRegistry->register('config_system_email_template', $collection); } $options = $collection->toOptionArray(); $templateId = str_replace('/', '_', $this->getPath()); $templateLabel = $this->_emailConfig->getTemplateLabel($templateId); $templateLabel = __('%1 (Default)', $templateLabel); array_unshift($options, ['value' => $templateId, 'label' => $templateLabel]); return $options; }
/** * @param $templateType string * @param $templateText string * @param $parsedTemplateText string * @param $expectedTemplateSubject string|null * @param $expectedOrigTemplateVariables array|null * @param $expectedTemplateStyles string|null * @dataProvider loadDefaultDataProvider */ public function testLoadDefault($templateType, $templateText, $parsedTemplateText, $expectedTemplateSubject, $expectedOrigTemplateVariables, $expectedTemplateStyles) { $model = $this->getModelMock(['getDesignParams']); $designParams = ['area' => Area::AREA_FRONTEND, 'theme' => 'Magento/blank', 'locale' => Locale::DEFAULT_SYSTEM_LOCALE]; $model->expects($this->once())->method('getDesignParams')->will($this->returnValue($designParams)); $templateId = 'templateId'; $templateFile = 'templateFile'; $this->emailConfig->expects($this->once())->method('getTemplateFilename')->with($templateId)->will($this->returnValue($templateFile)); $this->emailConfig->expects($this->once())->method('getTemplateType')->with($templateId)->will($this->returnValue($templateType)); $modulesDir = $this->getMockBuilder('Magento\\Framework\\Filesystem\\Directory\\ReadInterface')->setMethods(['readFile', 'getRelativePath'])->getMockForAbstractClass(); $relativePath = 'relativePath'; $modulesDir->expects($this->once())->method('getRelativePath')->with($templateFile)->will($this->returnValue($relativePath)); $modulesDir->expects($this->once())->method('readFile')->will($this->returnValue($templateText)); $this->filesystem->expects($this->once())->method('getDirectoryRead')->with(DirectoryList::ROOT)->will($this->returnValue($modulesDir)); $model->loadDefault($templateId); if ($templateType === 'html') { $this->assertEquals(TemplateTypesInterface::TYPE_HTML, $model->getTemplateType()); } else { $this->assertEquals(TemplateTypesInterface::TYPE_TEXT, $model->getTemplateType()); } $this->assertEquals($templateId, $model->getId()); $this->assertEquals($parsedTemplateText, $model->getTemplateText()); $this->assertEquals($expectedTemplateSubject, $model->getTemplateSubject()); $this->assertEquals($expectedOrigTemplateVariables, $model->getData('orig_template_variables')); $this->assertEquals($expectedTemplateStyles, $model->getTemplateStyles()); }
/** * Get default templates as options array * * @return array */ protected function _getDefaultTemplatesAsOptionsArray() { $options = array_merge([['value' => '', 'label' => '', 'group' => '']], $this->_emailConfig->getAvailableTemplates()); uasort($options, function (array $firstElement, array $secondElement) { return strcmp($firstElement['label'], $secondElement['label']); }); return $options; }
/** * Get default templates as options array * * @return array */ protected function _getDefaultTemplatesAsOptionsArray() { $options = [['value' => '', 'label' => '', 'group' => '']]; foreach ($this->_emailConfig->getAvailableTemplates() as $templateId) { $options[] = ['value' => $templateId, 'label' => $this->_emailConfig->getTemplateLabel($templateId), 'group' => $this->_emailConfig->getTemplateModule($templateId)]; } uasort($options, function (array $firstElement, array $secondElement) { return strcmp($firstElement['label'], $secondElement['label']); }); return $options; }
public function testGetDefaultTemplatesAsOptionsArray() { $directoryMock = $this->getMock('\\Magento\\Framework\\Filesystem\\Directory\\Read', [], [], '', false); $this->filesystemMock->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($directoryMock)); $this->_emailConfigMock->expects($this->once())->method('getAvailableTemplates')->will($this->returnValue([['value' => 'template_b2', 'label' => 'Template B2', 'group' => 'Fixture_ModuleB'], ['value' => 'template_a', 'label' => 'Template A', 'group' => 'Fixture_ModuleA'], ['value' => 'template_b1', 'label' => 'Template B1', 'group' => 'Fixture_ModuleB']])); $this->assertEmpty($this->_block->getData('template_options')); $this->_block->setTemplate('my/custom\\template.phtml'); $this->_block->toHtml(); $expectedResult = ['' => [['value' => '', 'label' => '', 'group' => '']], 'Fixture_ModuleA' => [['value' => 'template_a', 'label' => 'Template A', 'group' => 'Fixture_ModuleA']], 'Fixture_ModuleB' => [['value' => 'template_b1', 'label' => 'Template B1', 'group' => 'Fixture_ModuleB'], ['value' => 'template_b2', 'label' => 'Template B2', 'group' => 'Fixture_ModuleB']]]; $this->assertEquals($expectedResult, $this->_block->getData('template_options'), 'Options are expected to be sorted by modules and by labels of email templates within modules'); }
/** * Set template data to retrieve it in template info form * * @return void */ public function execute() { $this->_view->loadLayout(); $template = $this->_initTemplate('id'); $templateId = $this->getRequest()->getParam('code'); try { $parts = $this->emailConfig->parseTemplateIdParts($templateId); $templateId = $parts['templateId']; $theme = $parts['theme']; if ($theme) { $template->setForcedTheme($templateId, $theme); } $template->setForcedArea($templateId); $template->loadDefault($templateId); $template->setData('orig_template_code', $templateId); $template->setData('template_variables', \Zend_Json::encode($template->getVariablesOptionArray(true))); $templateBlock = $this->_view->getLayout()->createBlock('Magento\\Email\\Block\\Adminhtml\\Template\\Edit'); $template->setData('orig_template_currently_used_for', $templateBlock->getCurrentlyUsedForPaths(false)); $this->getResponse()->representJson($this->_objectManager->get('Magento\\Framework\\Json\\Helper\\Data')->jsonEncode($template->getData())); } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); } }
public function testGetDefaultTemplatesAsOptionsArray() { $dirValueMap = array(array(\Magento\Framework\App\Filesystem::ROOT_DIR, 'var/www/magento\\rootdir/'), array(\Magento\Framework\App\Filesystem::APP_DIR, 'var/www/magento\\rootdir\\app/'), array(\Magento\Framework\App\Filesystem::THEMES_DIR, 'var\\www/magento\\rootdir\\app/themes/')); $this->directoryMock = $this->getMock('\\Magento\\Framework\\Filesystem\\Directory\\Read', array(), array(), '', false); $this->directoryMock->expects($this->any())->method('isFile')->will($this->returnValue(false)); $this->directoryMock->expects($this->any())->method('getRelativePath')->will($this->returnValue('')); $this->filesystemMock->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($this->directoryMock)); $this->filesystemMock->expects($this->any())->method('getPath')->will($this->returnValueMap($dirValueMap)); $this->_emailConfigMock->expects($this->once())->method('getAvailableTemplates')->will($this->returnValue(array('template_b2', 'template_a', 'template_b1'))); $this->_emailConfigMock->expects($this->exactly(3))->method('getTemplateModule')->will($this->onConsecutiveCalls('Fixture_ModuleB', 'Fixture_ModuleA', 'Fixture_ModuleB')); $this->_emailConfigMock->expects($this->exactly(3))->method('getTemplateLabel')->will($this->onConsecutiveCalls('Template B2', 'Template A', 'Template B1')); $this->assertEmpty($this->_block->getData('template_options')); $this->_block->setTemplate('my/custom\\template.phtml'); $this->_block->toHtml(); $expectedResult = array('' => array(array('value' => '', 'label' => '', 'group' => '')), 'Fixture_ModuleA' => array(array('value' => 'template_a', 'label' => 'Template A', 'group' => 'Fixture_ModuleA')), 'Fixture_ModuleB' => array(array('value' => 'template_b1', 'label' => 'Template B1', 'group' => 'Fixture_ModuleB'), array('value' => 'template_b2', 'label' => 'Template B2', 'group' => 'Fixture_ModuleB'))); $this->assertEquals($expectedResult, $this->_block->getData('template_options'), 'Options are expected to be sorted by modules and by labels of email templates within modules'); }
/** * @param $templateType string * @param $templateText string * @param $parsedTemplateText string * @param $expectedTemplateSubject string|null * @param $expectedOrigTemplateVariables array|null * @param $expectedTemplateStyles string|null * @dataProvider loadDefaultDataProvider */ public function testLoadDefault($templateType, $templateText, $parsedTemplateText, $expectedTemplateSubject, $expectedOrigTemplateVariables, $expectedTemplateStyles) { $model = $this->getModelMock(); $templateId = 'templateId'; $templateFile = 'templateFile'; $this->emailConfig->expects($this->once())->method('getTemplateFilename')->with($templateId)->will($this->returnValue($templateFile)); $this->emailConfig->expects($this->once())->method('getTemplateType')->with($templateId)->will($this->returnValue($templateType)); $modulesDir = $this->getMockBuilder('Magento\\Framework\\Filesystem\\Directory\\ReadInterface')->setMethods(['readFile', 'getRelativePath'])->getMockForAbstractClass(); $relativePath = 'relativePath'; $modulesDir->expects($this->once())->method('getRelativePath')->with($templateFile)->will($this->returnValue($relativePath)); $modulesDir->expects($this->once())->method('readFile')->will($this->returnValue($templateText)); $this->filesystem->expects($this->once())->method('getDirectoryRead')->with(\Magento\Framework\App\Filesystem\DirectoryList::MODULES)->will($this->returnValue($modulesDir)); $model->loadDefault($templateId); if ($templateType === 'html') { $this->assertEquals(\Magento\Email\Model\Template::TYPE_HTML, $model->getTemplateType()); } else { $this->assertEquals(\Magento\Email\Model\Template::TYPE_TEXT, $model->getTemplateType()); } $this->assertEquals($templateId, $model->getId()); $this->assertEquals($parsedTemplateText, $model->getTemplateText()); $this->assertEquals($expectedTemplateSubject, $model->getTemplateSubject()); $this->assertEquals($expectedOrigTemplateVariables, $model->getData('orig_template_variables')); $this->assertEquals($expectedTemplateStyles, $model->getTemplateStyles()); }
/** * Manually set a theme that will be used by getParams * * Used to force the loading of an email template from a specific theme * * @param string $templateId * @param string $theme * @return $this */ public function setForcedTheme($templateId, $theme) { $area = $this->emailConfig->getTemplateArea($templateId); $this->design->setDesignTheme($theme, $area); return $this; }
/** * Getter for template type * * @return int|string */ public function getType() { $templateType = $this->getTemplateType(); if (is_null($templateType) && $this->getId()) { $templateType = $this->_emailConfig->getTemplateType($this->getId()); $templateType = $templateType == 'html' ? self::TYPE_HTML : self::TYPE_TEXT; } return $templateType; }
/** * Ensure that the getTemplateFilename method can be called without design params */ public function testGetTemplateFilenameWithNoParams() { $this->viewFileSystem->expects($this->once())->method('getEmailTemplateFileName')->with('one.html', ['area' => $this->designParams['area'], 'module' => $this->designParams['module']], 'Fixture_ModuleOne')->will($this->returnValue('_files/Fixture/ModuleOne/view/frontend/email/one.html')); $actualResult = $this->model->getTemplateFilename('template_one'); $this->assertEquals('_files/Fixture/ModuleOne/view/frontend/email/one.html', $actualResult); }
public function testGetTemplateFilename() { $this->_moduleReader->expects($this->once())->method('getModuleDir')->with('view', 'Fixture_ModuleOne')->will($this->returnValue('_files/Fixture/ModuleOne/view')); $actualResult = $this->_model->getTemplateFilename('template_one'); $this->assertEquals('_files/Fixture/ModuleOne/view/email/one.html', $actualResult); }
/** * Load default email template * * @param string $templateId * @return $this */ public function loadDefault($templateId) { $templateFile = $this->emailConfig->getTemplateFilename($templateId); $templateType = $this->emailConfig->getTemplateType($templateId); $templateTypeCode = $templateType == 'html' ? self::TYPE_HTML : self::TYPE_TEXT; $this->setTemplateType($templateTypeCode); $modulesDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MODULES); $templateText = $modulesDirectory->readFile($modulesDirectory->getRelativePath($templateFile)); /** * trim copyright message for text templates */ if ('html' != $templateType && preg_match('/^<!--[\\w\\W]+?-->/m', $templateText, $matches) && strpos($matches[0], 'Copyright') > 0) { $templateText = str_replace($matches[0], '', $templateText); } if (preg_match('/<!--@subject\\s*(.*?)\\s*@-->/u', $templateText, $matches)) { $this->setTemplateSubject($matches[1]); $templateText = str_replace($matches[0], '', $templateText); } if (preg_match('/<!--@vars\\s*((?:.)*?)\\s*@-->/us', $templateText, $matches)) { $this->setData('orig_template_variables', str_replace("\n", '', $matches[1])); $templateText = str_replace($matches[0], '', $templateText); } if (preg_match('/<!--@styles\\s*(.*?)\\s*@-->/s', $templateText, $matches)) { $this->setTemplateStyles($matches[1]); $templateText = str_replace($matches[0], '', $templateText); } // Remove comment lines and extra spaces $templateText = trim(preg_replace('#\\{\\*.*\\*\\}#suU', '', $templateText)); $this->setTemplateText($templateText); $this->setId($templateId); return $this; }