TemplateTest.php 28.6 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Email\Model;

use Magento\Backend\App\Area\FrontNameResolver as BackendFrontNameResolver;
use Magento\Framework\App\Area;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\TemplateTypesInterface;
use Magento\Framework\View\DesignInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use Magento\TestFramework\Helper\Bootstrap;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class TemplateTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var Template|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $model;

    /**
     * @var \Zend_Mail|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $mail;

    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;

    protected function setUp()
    {
        $this->objectManager = Bootstrap::getObjectManager();
    }

    protected function mockModel($filesystem = null)
    {
        if (!$filesystem) {
            $filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
        }

        $this->mail = $this->getMockBuilder(\Zend_Mail::class)
            ->setMethods(['send', 'addTo', 'addBcc', 'setReturnPath', 'setReplyTo'])
            ->setConstructorArgs(['utf-8'])
            ->getMock();

        $this->model = $this->getMockBuilder(\Magento\Email\Model\Template::class)
            ->setMethods(['_getMail'])
            ->setConstructorArgs([
                $this->objectManager->get(\Magento\Framework\Model\Context::class),
                $this->objectManager->get(\Magento\Framework\View\DesignInterface::class),
                $this->objectManager->get(\Magento\Framework\Registry::class),
                $this->objectManager->get(\Magento\Store\Model\App\Emulation::class),
                $this->objectManager->get(\Magento\Store\Model\StoreManager::class),
                $this->objectManager->create(\Magento\Framework\View\Asset\Repository::class),
                $filesystem,
                $this->objectManager->create(\Magento\Framework\App\Config\ScopeConfigInterface::class),
                $this->objectManager->get(\Magento\Email\Model\Template\Config::class),
                $this->objectManager->get(\Magento\Email\Model\TemplateFactory::class),
                $this->objectManager->get(\Magento\Framework\Filter\FilterManager::class),
                $this->objectManager->get(\Magento\Framework\UrlInterface::class),
                $this->objectManager->get(\Magento\Email\Model\Template\FilterFactory::class),
            ])
            ->getMock();

        $this->objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('frontend');

        $this->model->expects($this->any())->method('_getMail')->will($this->returnCallback([$this, 'getMail']));
        $this->model->setSenderName('sender')->setSenderEmail('sender@example.com')->setTemplateSubject('Subject');
    }

    /**
     * Return a disposable \Zend_Mail instance
     *
     * @return \PHPUnit_Framework_MockObject_MockObject|\Zend_Mail
     */
    public function getMail()
    {
        return clone $this->mail;
    }

    public function testSetGetTemplateFilter()
    {
        $this->mockModel();
        $filter = $this->model->getTemplateFilter();
        $this->assertSame($filter, $this->model->getTemplateFilter());
        $this->assertEquals(
            $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->getStore()->getId(),
            $filter->getStoreId()
        );

        $filter = $this->objectManager->create(\Magento\Email\Model\Template\Filter::class);
        $this->model->setTemplateFilter($filter);
        $this->assertSame($filter, $this->model->getTemplateFilter());
    }

    public function testLoadDefault()
    {
        $this->mockModel();
        $this->model->loadDefault('customer_create_account_email_template');
        $this->assertNotEmpty($this->model->getTemplateText());
        $this->assertNotEmpty($this->model->getTemplateSubject());
        $this->assertNotEmpty($this->model->getOrigTemplateVariables());
        $this->assertInternalType('array', json_decode($this->model->getOrigTemplateVariables(), true));
    }

    /**
     * @magentoAppIsolation enabled
     * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
     */
    public function testGetProcessedTemplate()
    {
        $this->mockModel();
        $this->objectManager->get(\Magento\Framework\App\AreaList::class)
            ->getArea(Area::AREA_FRONTEND)
            ->load();

        $expectedViewUrl = '/frontend/Magento/blank/en_US/Magento_Theme/favicon.ico';
        $this->model->setDesignConfig([
            'area' => 'frontend',
            'store' => $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
                ->getStore('fixturestore')
                ->getId(),
        ]);
        $this->model->setTemplateText('{{view url="Magento_Theme::favicon.ico"}}');

        $this->setNotDefaultThemeForFixtureStore();
        $this->assertStringEndsNotWith($expectedViewUrl, $this->model->getProcessedTemplate());

        $this->setDefaultThemeForFixtureStore();
        $this->assertStringEndsWith($expectedViewUrl, $this->model->getProcessedTemplate());
    }

    /**
     * Test template directive to ensure that templates can be loaded from modules
     *
     * @param string $area
     * @param string $templateId
     * @param string $expectedOutput
     * @param bool $mockThemeFallback
     *
     * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
     * @magentoComponentsDir Magento/Email/Model/_files/design
     * @magentoAppIsolation enabled
     * @magentoDbIsolation enabled
     * @dataProvider templateFallbackDataProvider
     */
    public function testTemplateFallback($area, $templateId, $expectedOutput, $mockThemeFallback = false)
    {
        $this->mockModel();

        if ($mockThemeFallback == BackendFrontNameResolver::AREA_CODE) {
            $this->setUpAdminThemeFallback();
        } elseif ($mockThemeFallback == Area::AREA_FRONTEND) {
            $this->setUpThemeFallback($area);
        }

        $this->model->setId($templateId);

        $this->assertContains($expectedOutput, $this->model->processTemplate());
    }

    /**
     * @return array
     */
    public function templateFallbackDataProvider()
    {
        return [
            'Template from module - admin' => [
                BackendFrontNameResolver::AREA_CODE,
                'customer_create_account_email_template',
                'To sign in to our site, use these credentials during checkout',
            ],
            'Template from module - frontend' => [
                Area::AREA_FRONTEND,
                'customer_create_account_email_template',
                'To sign in to our site, use these credentials during checkout',
            ],
            'Template from theme - frontend' => [
                Area::AREA_FRONTEND,
                'customer_create_account_email_template',
                'customer_create_account_email_template template from Vendor/custom_theme',
                Area::AREA_FRONTEND,
            ],
            'Template from parent theme - frontend' => [
                Area::AREA_FRONTEND,
                'customer_create_account_email_confirmation_template',
                'customer_create_account_email_confirmation_template template from Vendor/default',
                Area::AREA_FRONTEND,
            ],
            'Template from grandparent theme - frontend' => [
                Area::AREA_FRONTEND,
                'customer_create_account_email_confirmed_template',
                'customer_create_account_email_confirmed_template template from Magento/default',
                Area::AREA_FRONTEND,
            ],
            'Template from grandparent theme - adminhtml' => [
                BackendFrontNameResolver::AREA_CODE,
                'catalog_productalert_cron_error_email_template',
                'catalog_productalert_cron_error_email_template template from Magento/default',
                BackendFrontNameResolver::AREA_CODE,
            ],

        ];
    }

    /**
     * Test template directive to ensure that templates can be loaded from modules, overridden in backend, and
     * overridden in themes
     *
     * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
     * @magentoComponentsDir Magento/Email/Model/_files/design
     * @magentoAppIsolation enabled
     * @magentoDbIsolation enabled
     * @dataProvider templateDirectiveDataProvider
     *
     * @param string $area
     * @param int $templateType
     * @param string $templateText
     * @param string $assertContains
     * @param string $assertNotContains
     * @param string $storeConfigPath
     * @param bool $mockAdminTheme
     */
    public function testTemplateDirective(
        $area,
        $templateType,
        $templateText,
        $assertContains,
        $assertNotContains = null,
        $storeConfigPath = null,
        $mockAdminTheme = false
    ) {
        $this->mockModel();

        if ($mockAdminTheme) {
            $this->setUpAdminThemeFallback();
        } else {
            $this->setUpThemeFallback($area);
        }

        $this->model->setTemplateType($templateType);
        $this->model->setTemplateText($templateText);

        // Allows for testing of templates overridden in backend
        if ($storeConfigPath) {
            $template = $this->objectManager->create(\Magento\Email\Model\Template::class);
            $templateData = [
                'template_code' => 'some_unique_code',
                'template_type' => $templateType,
                'template_text' => $assertContains,
            ];
            $template->setData($templateData);
            $template->save();

            // Store the ID of the newly created template in the system config so that this template will be loaded
            $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
                ->setValue($storeConfigPath, $template->getId(), ScopeInterface::SCOPE_STORE, 'fixturestore');
        }

        $this->assertContains($assertContains, $this->model->getProcessedTemplate());
        if ($assertNotContains) {
            $this->assertNotContains($assertNotContains, $this->model->getProcessedTemplate());
        }
    }

    /**
     * @return array
     */
    public function templateDirectiveDataProvider()
    {
        return [
            'Template from module folder - adminhtml' => [
                BackendFrontNameResolver::AREA_CODE,
                TemplateTypesInterface::TYPE_HTML,
                '{{template config_path="design/email/footer_template"}}',
                "</table>\n<!-- End wrapper table -->",
            ],
            'Template from module folder - frontend' => [
                Area::AREA_FRONTEND,
                TemplateTypesInterface::TYPE_HTML,
                '{{template config_path="design/email/footer_template"}}',
                "</table>\n<!-- End wrapper table -->",
            ],
            'Template from module folder - plaintext' => [
                Area::AREA_FRONTEND,
                TemplateTypesInterface::TYPE_TEXT,
                '{{template config_path="design/email/footer_template"}}',
                'Thank you',
                "</table>\n<!-- End wrapper table -->",
            ],
            'Template overridden in backend - adminhtml' => [
                BackendFrontNameResolver::AREA_CODE,
                TemplateTypesInterface::TYPE_HTML,
                '{{template config_path="design/email/footer_template"}}',
                '<b>Footer configured in backend - email loaded via adminhtml</b>',
                null,
                'design/email/footer_template',
            ],
            'Template overridden in backend - frontend' => [
                Area::AREA_FRONTEND,
                TemplateTypesInterface::TYPE_HTML,
                '{{template config_path="design/email/footer_template"}}',
                '<b>Footer configured in backend - email loaded via frontend</b>',
                null,
                'design/email/footer_template',
            ],
            'Template from theme - frontend' => [
                Area::AREA_FRONTEND,
                TemplateTypesInterface::TYPE_HTML,
                '{{template config_path="customer/create_account/email_template"}}',
                '<strong>customer_create_account_email_template template from Vendor/custom_theme</strong>',
            ],
            'Template from parent theme - frontend' => [
                Area::AREA_FRONTEND,
                TemplateTypesInterface::TYPE_HTML,
                '{{template config_path="customer/create_account/email_confirmation_template"}}',
                '<strong>customer_create_account_email_confirmation_template template from Vendor/default</strong',
            ],
            'Template from grandparent theme - frontend' => [
                Area::AREA_FRONTEND,
                TemplateTypesInterface::TYPE_HTML,
                '{{template config_path="customer/create_account/email_confirmed_template"}}',
                '<strong>customer_create_account_email_confirmed_template template from Magento/default</strong',
            ],
            'Template from grandparent theme - adminhtml' => [
                BackendFrontNameResolver::AREA_CODE,
                TemplateTypesInterface::TYPE_HTML,
                '{{template config_path="catalog/productalert_cron/error_email_template"}}',
                '<strong>catalog_productalert_cron_error_email_template template from Magento/default</strong',
                null,
                null,
                true,
            ],
        ];
    }

    /**
     * Ensure that the template_styles variable contains styles from either <!--@styles @--> or the "Template Styles"
     * textarea in backend, depending on whether template was loaded from filesystem or DB.
     *
     * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
     * @magentoComponentsDir Magento/Email/Model/_files/design
     * @magentoAppIsolation enabled
     * @magentoDbIsolation enabled
     * @dataProvider templateStylesVariableDataProvider
     *
     * @param string $area
     * @param string $expectedOutput
     * @param array $unexpectedOutputs
     * @param array $templateForDatabase
     */
    public function testTemplateStylesVariable($area, $expectedOutput, $unexpectedOutputs, $templateForDatabase = [])
    {
        if (count($templateForDatabase)) {
            $this->mockModel();
            $this->setUpThemeFallback($area);

            $template = $this->objectManager->create(\Magento\Email\Model\Template::class);
            $template->setData($templateForDatabase);
            $template->save();
            $templateId = $template->getId();

            $this->model->load($templateId);
        } else {
            // <!--@styles @--> parsing only via the loadDefault method. Since email template files won't contain
            // @styles comments by default, it is necessary to mock an object to return testable contents
            $themeDirectory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
                ->disableOriginalConstructor()
                ->setMethods([
                    'readFile',
                ])
                ->getMockForAbstractClass();

            $themeDirectory->expects($this->once())
                ->method('readFile')
                ->will($this->returnValue('<!--@styles p { color: #111; } @--> {{var template_styles}}'));

            $filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
                ->disableOriginalConstructor()
                ->setMethods(['getDirectoryRead'])
                ->getMock();

            $filesystem->expects($this->once())
                ->method('getDirectoryRead')
                ->with(DirectoryList::ROOT)
                ->will($this->returnValue($themeDirectory));

            $this->mockModel($filesystem);

            $this->model->loadDefault('design_email_header_template');
        }

        $processedTemplate = $this->model->getProcessedTemplate();

        foreach ($unexpectedOutputs as $unexpectedOutput) {
            $this->assertNotContains($unexpectedOutput, $processedTemplate);
        }

        $this->assertContains($expectedOutput, $processedTemplate);
    }

    /**
     * @return array
     */
    public function templateStylesVariableDataProvider()
    {
        return [
            'Styles from <!--@styles @--> comment - adminhtml' => [
                BackendFrontNameResolver::AREA_CODE,
                'p { color: #111; }',
                [
                    '<!--@styles',
                    '{{var template_styles}}',
                ],
            ],
            'Styles from <!--@styles @--> comment - frontend' => [
                Area::AREA_FRONTEND,
                'p { color: #111; }',
                [
                    '<!--@styles',
                    '{{var template_styles}}',
                ],
            ],
            'Styles from "Template Styles" textarea from backend - adminhtml' => [
                BackendFrontNameResolver::AREA_CODE,
                'p { color: #222; }',
                ['{{var template_styles}}'],
                [
                    'template_code' => 'some_unique_code',
                    'template_type' => Template::TYPE_HTML,
                    'template_text' => 'Footer from database {{var template_styles}}',
                    'template_styles' => 'p { color: #222; }',
                ],
            ],
            'Styles from "Template Styles" textarea from backend - frontend' => [
                Area::AREA_FRONTEND,
                'p { color: #333; }',
                ['{{var template_styles}}'],
                [
                    'template_code' => 'some_unique_code',
                    'template_type' => Template::TYPE_HTML,
                    'template_text' => 'Footer from database {{var template_styles}}',
                    'template_styles' => 'p { color: #333; }',
                ],
            ],
        ];
    }

    /**
     * Setup the theme fallback structure and set the Vendor_EmailTest/custom_theme as the current theme for
     * 'fixturestore' store
     */
    protected function setUpAdminThemeFallback()
    {
        $themes = [BackendFrontNameResolver::AREA_CODE => 'Vendor_EmailTest/custom_theme'];
        $design = $this->objectManager->create(\Magento\Theme\Model\View\Design::class, ['themes' => $themes]);
        $this->objectManager->addSharedInstance($design, \Magento\Theme\Model\View\Design::class);
        /** @var \Magento\Theme\Model\Theme\Registration $registration */
        $registration = $this->objectManager->get(
            \Magento\Theme\Model\Theme\Registration::class
        );
        $registration->register();

        // The Vendor_EmailTest/custom_theme adminhtml theme is set in the
        // dev/tests/integration/testsuite/Magento/Email/Model/_files/design/themes.php file, as it must be set
        // before the adminhtml area is loaded below.

        Bootstrap::getInstance()->loadArea(BackendFrontNameResolver::AREA_CODE);

        /** @var \Magento\Store\Model\Store $adminStore */
        $adminStore = $this->objectManager->create(\Magento\Store\Model\Store::class)
            ->load(Store::ADMIN_CODE);

        $this->model->setDesignConfig([
            'area' => 'adminhtml',
            'store' => $adminStore->getId(),
        ]);
    }

    /**
     * Setup the theme fallback structure and set the Vendor_EmailTest/custom_theme as the current theme
     * for 'fixturestore' store
     *
     * @param $area
     */
    protected function setUpThemeFallback($area)
    {
        /** @var \Magento\Theme\Model\Theme\Registration $registration */
        $registration = $this->objectManager->get(
            \Magento\Theme\Model\Theme\Registration::class
        );
        $registration->register();

        // It is important to test from both areas, as emails will get sent from both, so we need to ensure that the
        // inline CSS files get loaded properly from both areas.
        Bootstrap::getInstance()->loadArea($area);

        $collection = $this->objectManager->create(\Magento\Theme\Model\ResourceModel\Theme\Collection::class);

        // Hard-coding theme as we want to test the fallback structure to ensure that the parent/grandparent themes of
        // Vendor_EmailTest/custom_theme will be checked for CSS files
        $themeId = $collection->getThemeByFullPath('frontend/Vendor_EmailTest/custom_theme')->getId();

        $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
            ->setValue(
                DesignInterface::XML_PATH_THEME_ID,
                $themeId,
                ScopeInterface::SCOPE_STORE,
                'fixturestore'
            );

        $this->model->setDesignConfig([
            'area' => 'frontend',
            'store' => $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
                ->getStore('fixturestore')
                ->getId(),
        ]);
    }

    /**
     * Set 'Magento/luma' for the 'fixturestore' store.
     * Application isolation is required, if a test uses this method.
     */
    protected function setNotDefaultThemeForFixtureStore()
    {
        /** @var \Magento\Framework\View\Design\ThemeInterface $theme */
        $theme = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
            \Magento\Framework\View\Design\ThemeInterface::class
        );
        $theme->load('Magento/luma', 'theme_path');
        \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
            \Magento\Framework\App\Config\MutableScopeConfigInterface::class
        )->setValue(
            \Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID,
            $theme->getId(),
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            'fixturestore'
        );
    }

    /**
     * Set 'Magento/blank' for the 'fixturestore' store.
     * Application isolation is required, if a test uses this method.
     */
    protected function setDefaultThemeForFixtureStore()
    {
        /** @var \Magento\Framework\View\Design\ThemeInterface $theme */
        $theme = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
            \Magento\Framework\View\Design\ThemeInterface::class
        );
        $theme->load('Magento/blank', 'theme_path');
        \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
            \Magento\Framework\App\Config\MutableScopeConfigInterface::class
        )->setValue(
            \Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID,
            $theme->getId(),
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            'fixturestore'
        );
    }

    /**
     * @magentoAppIsolation enabled
     * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
     */
    public function testGetProcessedTemplateSubject()
    {
        $this->mockModel();
        $this->objectManager->get(\Magento\Framework\App\AreaList::class)
            ->getArea(Area::AREA_FRONTEND)
            ->load();

        $this->model->setTemplateSubject('{{view url="Magento_Theme::favicon.ico"}}');
        $this->model->setDesignConfig([
            'area' => 'frontend',
            'store' => $this->objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
                ->getStore('fixturestore')
                ->getId(),
        ]);

        $this->setNotDefaultThemeForFixtureStore();
        $this->assertStringMatchesFormat(
            '%s/frontend/Magento/luma/en_US/Magento_Theme/favicon.ico',
            $this->model->getProcessedTemplateSubject([])
        );

        $this->setDefaultThemeForFixtureStore();
        $this->assertStringMatchesFormat(
            '%s/frontend/Magento/blank/en_US/Magento_Theme/favicon.ico',
            $this->model->getProcessedTemplateSubject([])
        );
    }

    /**
     * @magentoAppIsolation enabled
     */
    public function testGetDefaultEmailLogo()
    {
        $this->mockModel();
        $this->objectManager->get(\Magento\Framework\App\AreaList::class)
            ->getArea(Area::AREA_FRONTEND)
            ->load();

        $this->assertStringEndsWith(
            '/frontend/Magento/luma/en_US/Magento_Email/logo_email.png',
            $this->model->getDefaultEmailLogo()
        );
    }

    /**
     * @param $config
     * @dataProvider setDesignConfigExceptionDataProvider
     * @expectedException \Magento\Framework\Exception\LocalizedException
     */
    public function testSetDesignConfigException($config)
    {
        $this->mockModel();
        $model = $this->objectManager->create(\Magento\Email\Model\Template::class);
        $model->setDesignConfig($config);
    }

    public function setDesignConfigExceptionDataProvider()
    {
        $storeId = Bootstrap::getObjectManager()->get(\Magento\Store\Model\StoreManagerInterface::class)
            ->getStore()
            ->getId();

        return [
            [[]],
            [['area' => 'frontend']],
            [['store' => $storeId]],
        ];
    }

    public function testSetAndGetId()
    {
        $this->mockModel();
        $testId = 9999;
        $this->model->setId($testId);
        $this->assertEquals($testId, $this->model->getId());
    }

    public function testIsValidForSend()
    {
        $this->mockModel();
        $this->assertTrue($this->model->isValidForSend());
    }

    /**
     * @expectedException \UnexpectedValueException
     * @expectedExceptionMessage Email template 'foo' is not defined.
     */
    public function testGetTypeNonExistentType()
    {
        $this->mockModel();
        $this->model->setId('foo');
        $this->model->getType();
    }

    public function testGetTypeHtml()
    {
        $this->mockModel();
        $this->model->setId('customer_create_account_email_template');
        $this->assertEquals(TemplateTypesInterface::TYPE_HTML, $this->model->getType());
    }

    public function testGetType()
    {
        $this->mockModel();
        $templateTypeId = 'test_template';
        $this->model->setTemplateType($templateTypeId);
        $this->assertEquals($templateTypeId, $this->model->getType());
    }

    public function testGetSendingException()
    {
        $this->mockModel();
        $this->assertNull($this->model->getSendingException());
    }

    public function testGetVariablesOptionArray()
    {
        $this->mockModel();
        $testTemplateVariables = '{"var data.name":"Sender Name","var data.email":"Sender Email"}';
        $this->model->setOrigTemplateVariables($testTemplateVariables);
        $variablesOptionArray = $this->model->getVariablesOptionArray();
        $this->assertEquals('{{var data.name}}', $variablesOptionArray[0]['value']);
        $this->assertEquals('Sender Name', $variablesOptionArray[0]['label']->getArguments()[0]);
        $this->assertEquals('{{var data.email}}', $variablesOptionArray[1]['value']);
        $this->assertEquals('Sender Email', $variablesOptionArray[1]['label']->getArguments()[0]);
    }

    public function testGetVariablesOptionArrayInGroup()
    {
        $this->mockModel();
        $testTemplateVariables = '{"var data.name":"Sender Name","var data.email":"Sender Email"}';
        $this->model->setOrigTemplateVariables($testTemplateVariables);
        $variablesOptionArray = $this->model->getVariablesOptionArray(true);
        $this->assertEquals('Template Variables', $variablesOptionArray['label']->getText());
        $this->assertEquals($this->model->getVariablesOptionArray(), $variablesOptionArray['value']);
    }

    /**
     * @expectedException \Magento\Framework\Exception\MailException
     * @expectedExceptionMessage Please enter a template name.
     */
    public function testBeforeSaveEmptyTemplateCode()
    {
        $this->mockModel();
        $this->model->beforeSave();
    }

    public function testBeforeSave()
    {
        $this->mockModel();
        $this->model->setTemplateCode('test template code');
        $this->model->beforeSave();
    }

    public function testProcessTemplate()
    {
        $this->mockModel();
        $this->model->setId('customer_create_account_email_template');
        $this->assertContains('<body', $this->model->processTemplate());
    }

    public function testGetSubject()
    {
        $this->mockModel();
        $this->model->setVars(['foo', 'bar', 'baz']);
        $this->assertEquals('Subject', $this->model->getSubject());
    }

    public function testSetOptions()
    {
        $this->mockModel();
        $options = ['area' => 'test area', 'store' => 1];
        $this->model->setOptions($options);
        $this->assertEquals($options, $this->model->getDesignConfig()->getData());
    }
}