Skip to content

Commit bf5ac50

Browse files
committed
Add tests for Link formatin
1 parent 2a8fe98 commit bf5ac50

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

tests/Helpers/Formatting/LinkTest.php

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<?php
2+
3+
namespace Services;
4+
5+
use BEA\Theme\Framework\Service_Container;
6+
use BEA\Theme\Framework\Services\Svg;
7+
use WP_Mock;
8+
use WP_Mock\Tools\TestCase;
9+
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_attribute_value;
10+
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_content_value;
11+
use function BEA\Theme\Framework\Helpers\Formatting\Link\get_acf_link;
12+
use function BEA\Theme\Framework\Helpers\Formatting\Link\get_the_link;
13+
use function BEA\Theme\Framework\Helpers\Formatting\Text\get_the_text;
14+
use function BEA\Theme\Framework\Helpers\Formatting\Text\the_text;
15+
use function ob_get_clean;
16+
use function ob_start;
17+
use function var_dump;
18+
19+
class LinkTest extends TestCase {
20+
public function setUp(): void {
21+
WP_Mock::setUp();
22+
WP_Mock::passthruFunction( 'esc_html__' );
23+
}
24+
25+
public function tearDown(): void {
26+
WP_Mock::tearDown();
27+
}
28+
29+
public function testGetTheLinkEmpty() {
30+
$this->assertSame( '', get_the_link( [] ) );
31+
}
32+
33+
public function testGetTheLinkTargetBlank() {
34+
35+
// Auto noopener
36+
$this->assertSame(
37+
'<a title="TITLE LINK" target="_blank" href="https://localhost.dev" rel="noopener"><span class="sr-only">New window</span>TITLE LINK</a>',
38+
get_the_link(
39+
[
40+
'href' => 'https://localhost.dev',
41+
'target' => '_blank',
42+
'title' => 'TITLE LINK',
43+
]
44+
)
45+
);
46+
47+
// self
48+
$this->assertSame(
49+
'<a title="" target="_self" href="https://localhost.dev"></a>',
50+
get_the_link(
51+
[
52+
'href' => 'https://localhost.dev',
53+
'target' => '_self',
54+
]
55+
)
56+
);
57+
58+
// self title
59+
$this->assertSame(
60+
'<a title="TITLE LINK" target="_self" href="https://localhost.dev">TITLE LINK</a>',
61+
get_the_link(
62+
[
63+
'href' => 'https://localhost.dev',
64+
'target' => '_self',
65+
'title' => 'TITLE LINK',
66+
]
67+
)
68+
);
69+
70+
// Custom attribute
71+
$this->assertSame(
72+
'<a title="" target="" href="https://localhost.dev" data-seo="ok"></a>',
73+
get_the_link(
74+
[
75+
'href' => 'https://localhost.dev',
76+
'data-seo' => 'ok',
77+
]
78+
)
79+
);
80+
$this->assertSame(
81+
'<a title="" target="" href="https://localhost.dev" empty-data></a>',
82+
get_the_link(
83+
[
84+
'href' => 'https://localhost.dev',
85+
'empty-data' => null,
86+
]
87+
)
88+
);
89+
90+
// Before/after
91+
$this->assertSame(
92+
'b<a title="" target="" href="https://localhost.dev"></a>',
93+
get_the_link(
94+
[
95+
'href' => 'https://localhost.dev',
96+
],
97+
[
98+
'before' => 'b',
99+
]
100+
)
101+
);
102+
$this->assertSame(
103+
'<a title="" target="" href="https://localhost.dev"></a>a',
104+
get_the_link(
105+
[
106+
'href' => 'https://localhost.dev',
107+
],
108+
[
109+
'after' => 'a',
110+
]
111+
)
112+
);
113+
$this->assertSame(
114+
'b<a title="" target="" href="https://localhost.dev"></a>a',
115+
get_the_link(
116+
[
117+
'href' => 'https://localhost.dev',
118+
],
119+
[
120+
'after' => 'a',
121+
'before' => 'b',
122+
]
123+
)
124+
);
125+
126+
$this->assertSame(
127+
'<a title="" target="" href="https://localhost.dev">Content</a>',
128+
get_the_link(
129+
[
130+
'href' => 'https://localhost.dev',
131+
],
132+
[
133+
'content' => 'Content',
134+
]
135+
)
136+
);
137+
138+
$this->assertSame(
139+
'<a title="TITLE" target="" href="https://localhost.dev">Content</a>',
140+
get_the_link(
141+
[
142+
'href' => 'https://localhost.dev',
143+
'title' => 'TITLE',
144+
],
145+
[
146+
'content' => 'Content',
147+
]
148+
)
149+
);
150+
}
151+
152+
public function testGetAcfLinkEmptyURLOrTitle() {
153+
$this->assertSame( '', get_acf_link( [] ) );
154+
$this->assertSame( '', get_acf_link( [ 'field' => [ 'url' => 'ok' ] ] ) );
155+
$this->assertSame( '', get_acf_link( [ 'field' => [ 'title' => 'ok' ] ] ) );
156+
}
157+
158+
public function testGetAcfLinkWithAttributes() {
159+
$this->assertSame(
160+
'<a title="Title" target="_blank" href="https://localhost.dev" rel="noopener"><span class="sr-only">New window</span>Title</a>',
161+
get_acf_link(
162+
[
163+
'field' => [
164+
'title' => 'Title',
165+
'url' => 'https://localhost.dev',
166+
'target' => '_blank',
167+
],
168+
]
169+
)
170+
);
171+
172+
$this->assertSame(
173+
'<a title="Title" target="" href="https://localhost.dev">Title</a>',
174+
get_acf_link(
175+
[
176+
'field' => [
177+
'title' => 'Title',
178+
'url' => 'https://localhost.dev',
179+
'target' => '',
180+
],
181+
]
182+
)
183+
);
184+
185+
$this->assertSame(
186+
'<a title="Title" target="" href="https://localhost.dev">CONTENT</a>',
187+
get_acf_link(
188+
[
189+
'field' => [
190+
'title' => 'Title',
191+
'url' => 'https://localhost.dev',
192+
'target' => '',
193+
],
194+
],
195+
[
196+
'content' => 'CONTENT',
197+
]
198+
)
199+
);
200+
}
201+
}
202+

0 commit comments

Comments
 (0)