-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathBladeGettextExtractorTest.php
More file actions
143 lines (112 loc) · 4.17 KB
/
BladeGettextExtractorTest.php
File metadata and controls
143 lines (112 loc) · 4.17 KB
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
<?php
namespace WP_CLI\I18n\Tests;
use Gettext\Translations;
use WP_CLI\I18n\BladeCodeExtractor;
use WP_CLI\Tests\TestCase;
class BladeGettextExtractorTest extends TestCase {
/**
* Helper to extract translations from a Blade string.
*
* @param string $blade Blade template content.
* @param string $domain Text domain.
* @return Translations
*/
private function extract( $blade, $domain = 'foo-theme' ) {
$translations = new Translations();
$translations->setDomain( $domain );
$options = array_merge(
BladeCodeExtractor::$options,
[ 'file' => 'test.blade.php' ]
);
BladeCodeExtractor::fromString( $blade, $translations, $options );
return $translations;
}
public function test_extracts_bound_prop_with_translation_function() {
$translations = $this->extract(
'<x-alert :message="__(\'Hello\', \'foo-theme\')" />'
);
$this->assertNotFalse( $translations->find( null, 'Hello' ) );
}
public function test_extracts_multiple_bound_props() {
$translations = $this->extract(
'<x-no-results :title="__(\'Not found\', \'foo-theme\')" :subtitle="__(\'Try again\', \'foo-theme\')" />'
);
$this->assertNotFalse( $translations->find( null, 'Not found' ) );
$this->assertNotFalse( $translations->find( null, 'Try again' ) );
}
public function test_extracts_bound_props_from_multiline_component_tag() {
$blade = <<<'BLADE'
<x-no-results
:title="__('Page not found', 'foo-theme')"
:subtitle="esc_html__('Please try again', 'foo-theme')"
/>
BLADE;
$translations = $this->extract( $blade );
$this->assertNotFalse( $translations->find( null, 'Page not found' ) );
$this->assertNotFalse( $translations->find( null, 'Please try again' ) );
}
public function test_extracts_bound_props_from_open_component_tag() {
$blade = <<<'BLADE'
<x-alert :message="__('Warning message', 'foo-theme')">
{!! __('Content inside', 'foo-theme') !!}
</x-alert>
BLADE;
$translations = $this->extract( $blade );
$this->assertNotFalse( $translations->find( null, 'Warning message' ) );
$this->assertNotFalse( $translations->find( null, 'Content inside' ) );
}
public function test_ignores_static_props() {
$translations = $this->extract(
'<x-alert type="warning" :message="__(\'Hello\', \'foo-theme\')" />'
);
$this->assertNotFalse( $translations->find( null, 'Hello' ) );
$this->assertFalse( $translations->find( null, 'warning' ) );
}
public function test_does_not_match_non_component_html() {
$translations = $this->extract(
'<a href="https://example.com">{{ __(\'Link text\', \'foo-theme\') }}</a>'
);
$this->assertNotFalse( $translations->find( null, 'Link text' ) );
// Only 1 translation should exist.
$this->assertCount( 1, $translations );
}
public function test_extracts_context_function_in_prop() {
$translations = $this->extract(
'<x-button :label="_x(\'Read\', \'verb\', \'foo-theme\')" />'
);
$translation = $translations->find( 'verb', 'Read' );
$this->assertNotFalse( $translation );
}
public function test_extracts_esc_functions_in_props() {
$blade = <<<'BLADE'
<x-field
:label="esc_html__('Username', 'foo-theme')"
:placeholder="esc_attr__('Enter username', 'foo-theme')"
/>
BLADE;
$translations = $this->extract( $blade );
$this->assertNotFalse( $translations->find( null, 'Username' ) );
$this->assertNotFalse( $translations->find( null, 'Enter username' ) );
}
public function test_extracts_single_quoted_bound_props() {
$translations = $this->extract(
"<x-alert :message='__(\"Single quoted\", \"foo-theme\")' />"
);
$this->assertNotFalse( $translations->find( null, 'Single quoted' ) );
}
public function test_existing_blade_extraction_still_works() {
$blade = <<<'BLADE'
@php
__('PHP block string', 'foo-theme');
@endphp
{{ __('Echo string', 'foo-theme') }}
{!! __('Raw string', 'foo-theme') !!}
@php(__('Directive string', 'foo-theme'))
BLADE;
$translations = $this->extract( $blade );
$this->assertNotFalse( $translations->find( null, 'PHP block string' ) );
$this->assertNotFalse( $translations->find( null, 'Echo string' ) );
$this->assertNotFalse( $translations->find( null, 'Raw string' ) );
$this->assertNotFalse( $translations->find( null, 'Directive string' ) );
}
}