Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / blazy / tests / src / Unit / Form / BlazyAdminFormatterUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Unit\Form;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\blazy\Form\BlazyAdminFormatter;
7 use Drupal\blazy\Dejavu\BlazyDefault;
8 use Drupal\Tests\UnitTestCase;
9 use Drupal\Tests\blazy\Traits\BlazyUnitTestTrait;
10 use Drupal\Tests\blazy\Traits\BlazyManagerUnitTestTrait;
11
12 /**
13  * Tests the Blazy admin formatter form.
14  *
15  * @coversDefaultClass \Drupal\blazy\Form\BlazyAdminFormatter
16  * @group blazy
17  */
18 class BlazyAdminFormatterUnitTest extends UnitTestCase {
19
20   use BlazyUnitTestTrait;
21   use BlazyManagerUnitTestTrait;
22
23   /**
24    * The mocked translator.
25    *
26    * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
27    */
28   protected $stringTranslation;
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35
36     $this->setUpUnitServices();
37     $this->setUpUnitContainer();
38
39     $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface');
40     $this->entityDisplayRepository = $this->getMock('Drupal\Core\Entity\EntityDisplayRepositoryInterface');
41     $this->typedConfig = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
42     $this->dateFormatter = $this->getMockBuilder('Drupal\Core\Datetime\DateFormatter')
43       ->disableOriginalConstructor()
44       ->getMock();
45
46     $container = new ContainerBuilder();
47     $container->set('entity_display.repository', $this->entityDisplayRepository);
48     $container->set('config.typed', $this->typedConfig);
49     $container->set('string_translation', $this->getStringTranslationStub());
50     $container->set('date.formatter', $this->dateFormatter);
51
52     \Drupal::setContainer($container);
53
54     $this->blazyAdminFormatter = new BlazyAdminFormatter(
55       $this->entityDisplayRepository,
56       $this->typedConfig,
57       $this->blazyManager
58     );
59   }
60
61   /**
62    * @covers ::buildSettingsForm
63    * @covers ::openingForm
64    * @covers ::imageStyleForm
65    * @covers ::mediaSwitchForm
66    * @covers ::gridForm
67    * @covers ::breakpointsForm
68    * @covers ::closingForm
69    * @covers ::finalizeForm
70    */
71   public function testBuildSettingsForm() {
72     $form = [];
73     $definition = $this->getDefaulEntityFormatterDefinition() + $this->getDefaultFormatterDefinition();
74
75     $definition['settings'] += $this->getDefaultFields(TRUE);
76
77     $this->blazyAdminFormatter->buildSettingsForm($form, $definition);
78     $this->assertArrayHasKey('closing', $form);
79   }
80
81   /**
82    * Provider for ::testGetSettingsSummary.
83    */
84   public function providerTestGetSettingsSummary() {
85     return [
86       [FALSE, FALSE, FALSE, '', FALSE],
87       [TRUE, TRUE, TRUE, 'blazy_responsive_test', TRUE],
88       [TRUE, FALSE, FALSE, '', TRUE],
89     ];
90   }
91
92   /**
93    * Tests the Blazy admin ::getSettingsSummary().
94    *
95    * @dataProvider providerTestGetSettingsSummary
96    */
97   public function testGetSettingsSummary($use_settings, $vanilla, $override, $responsive_image_style, $expected) {
98     $definition = $this->getFormatterDefinition();
99     $settings = array_merge(BlazyDefault::gridSettings(), $definition['settings']);
100
101     $settings['vanilla']                = $vanilla;
102     $settings['image_syle']             = 'large';
103     $settings['box_style']              = 'blazy_crop';
104     $settings['thumbnail_style']        = 'thumbnail';
105     $settings['optionset']              = 'default';
106     $settings['override']               = $override;
107     $settings['overridables']           = ['foo' => 'foo', 'bar' => '0'];
108     $settings['responsive_image_style'] = $responsive_image_style;
109     $settings['caption']                = ['alt' => 'alt', 'title' => 'title'];
110     $settings['breakpoints']            = $this->getDataBreakpoints(TRUE);
111
112     $definition['settings'] = $use_settings ? $settings : [];
113
114     $summary = $this->blazyAdminFormatter->getSettingsSummary($definition);
115     $summary = array_filter($summary);
116     $check_summary = !$expected ? empty($summary) : !empty($summary);
117
118     $this->assertTrue($check_summary);
119   }
120
121 }