summaryrefslogtreecommitdiffstats
path: root/core/tests/Drupal/KernelTests/Core/Layout/IconBuilderTest.php
blob: 92ac3cec7a25d41705d926daf5ef9a85895c7d81 (plain)
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
<?php

namespace Drupal\KernelTests\Core\Layout;

use Drupal\Core\Layout\Icon\SvgIconBuilder;
use Drupal\Core\Render\RenderContext;
use Drupal\KernelTests\KernelTestBase;

/**
 * @coversDefaultClass \Drupal\Core\Layout\Icon\SvgIconBuilder
 * @group Layout
 */
class IconBuilderTest extends KernelTestBase {

  /**
   * @covers ::build
   * @covers ::buildRenderArray
   * @covers ::calculateSvgValues
   * @covers ::getLength
   * @covers ::getOffset
   *
   * @dataProvider providerTestBuild
   */
  public function testBuild(SvgIconBuilder $icon_builder, $icon_map, $expected) {
    $renderer = $this->container->get('renderer');

    $build = $icon_builder->build($icon_map);

    $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($build, $renderer) {
      return $renderer->render($build);
    });
    $this->assertSame($expected, $output);
  }

  public function providerTestBuild() {
    $data = [];
    $data['empty'][] = (new SvgIconBuilder());
    $data['empty'][] = [];
    $data['empty'][] = <<<'EOD'
<svg width="125" height="150" class="layout-icon"></svg>

EOD;

    $data['two_column'][] = (new SvgIconBuilder())
      ->setId('two_column')
      ->setLabel('Two Column')
      ->setWidth(250)
      ->setHeight(300)
      ->setStrokeWidth(2);
    $data['two_column'][] = [['left', 'right']];
    $data['two_column'][] = <<<'EOD'
<svg width="250" height="300" class="layout-icon layout-icon--two-column"><title>Two Column</title>
<g><title>left</title>
<rect x="1" y="1" width="121" height="298" stroke-width="2" class="layout-icon__region layout-icon__region--left" />
</g>
<g><title>right</title>
<rect x="128" y="1" width="121" height="298" stroke-width="2" class="layout-icon__region layout-icon__region--right" />
</g>
</svg>

EOD;

    $data['two_column_no_stroke'][] = (new SvgIconBuilder())
      ->setWidth(250)
      ->setHeight(300)
      ->setStrokeWidth(NULL);
    $data['two_column_no_stroke'][] = [['left', 'right']];
    $data['two_column_no_stroke'][] = <<<'EOD'
<svg width="250" height="300" class="layout-icon"><g><title>left</title>
<rect x="0" y="0" width="123" height="300" class="layout-icon__region layout-icon__region--left" />
</g>
<g><title>right</title>
<rect x="127" y="0" width="123" height="300" class="layout-icon__region layout-icon__region--right" />
</g>
</svg>

EOD;

    $data['two_column_border_collapse'][] = (new SvgIconBuilder())
      ->setWidth(250)
      ->setHeight(300)
      ->setStrokeWidth(2)
      ->setPadding(-2);
    $data['two_column_border_collapse'][] = [['left', 'right']];
    $data['two_column_border_collapse'][] = <<<'EOD'
<svg width="250" height="300" class="layout-icon"><g><title>left</title>
<rect x="1" y="1" width="124" height="298" stroke-width="2" class="layout-icon__region layout-icon__region--left" />
</g>
<g><title>right</title>
<rect x="125" y="1" width="124" height="298" stroke-width="2" class="layout-icon__region layout-icon__region--right" />
</g>
</svg>

EOD;

    $data['stacked'][] = (new SvgIconBuilder())
      ->setStrokeWidth(2);
    $data['stacked'][] = [
      ['sidebar', 'top', 'top'],
      ['sidebar', 'left', 'right'],
      ['sidebar', 'middle', 'middle'],
      ['footer_left', 'footer_right'],
      ['footer_full'],
    ];
    $data['stacked'][] = <<<'EOD'
<svg width="125" height="150" class="layout-icon"><g><title>sidebar</title>
<rect x="1" y="1" width="37" height="86.4" stroke-width="2" class="layout-icon__region layout-icon__region--sidebar" />
</g>
<g><title>top</title>
<rect x="44" y="1" width="80" height="24.8" stroke-width="2" class="layout-icon__region layout-icon__region--top" />
</g>
<g><title>left</title>
<rect x="44" y="31.8" width="37" height="24.8" stroke-width="2" class="layout-icon__region layout-icon__region--left" />
</g>
<g><title>right</title>
<rect x="87" y="31.8" width="37" height="24.8" stroke-width="2" class="layout-icon__region layout-icon__region--right" />
</g>
<g><title>middle</title>
<rect x="44" y="62.6" width="80" height="24.8" stroke-width="2" class="layout-icon__region layout-icon__region--middle" />
</g>
<g><title>footer_left</title>
<rect x="1" y="93.4" width="58.5" height="24.8" stroke-width="2" class="layout-icon__region layout-icon__region--footer-left" />
</g>
<g><title>footer_right</title>
<rect x="65.5" y="93.4" width="58.5" height="24.8" stroke-width="2" class="layout-icon__region layout-icon__region--footer-right" />
</g>
<g><title>footer_full</title>
<rect x="1" y="124.2" width="123" height="24.8" stroke-width="2" class="layout-icon__region layout-icon__region--footer-full" />
</g>
</svg>

EOD;

    return $data;
  }

}