|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WP_CLI\I18n\Tests; |
| 4 | + |
| 5 | +use Gettext\Translations; |
| 6 | +use WP_CLI\I18n\BladeCodeExtractor; |
| 7 | +use WP_CLI\Tests\TestCase; |
| 8 | + |
| 9 | +class BladeGettextExtractorTest extends TestCase { |
| 10 | + |
| 11 | + /** |
| 12 | + * Helper to extract translations from a Blade string. |
| 13 | + * |
| 14 | + * @param string $blade Blade template content. |
| 15 | + * @param string $domain Text domain. |
| 16 | + * @return Translations |
| 17 | + */ |
| 18 | + private function extract( $blade, $domain = 'foo-theme' ) { |
| 19 | + $translations = new Translations(); |
| 20 | + $translations->setDomain( $domain ); |
| 21 | + |
| 22 | + $options = array_merge( |
| 23 | + BladeCodeExtractor::$options, |
| 24 | + [ 'file' => 'test.blade.php' ] |
| 25 | + ); |
| 26 | + |
| 27 | + BladeCodeExtractor::fromString( $blade, $translations, $options ); |
| 28 | + |
| 29 | + return $translations; |
| 30 | + } |
| 31 | + |
| 32 | + public function test_extracts_bound_prop_with_translation_function() { |
| 33 | + $translations = $this->extract( |
| 34 | + '<x-alert :message="__(\'Hello\', \'foo-theme\')" />' |
| 35 | + ); |
| 36 | + |
| 37 | + $this->assertNotFalse( $translations->find( null, 'Hello' ) ); |
| 38 | + } |
| 39 | + |
| 40 | + public function test_extracts_multiple_bound_props() { |
| 41 | + $translations = $this->extract( |
| 42 | + '<x-no-results :title="__(\'Not found\', \'foo-theme\')" :subtitle="__(\'Try again\', \'foo-theme\')" />' |
| 43 | + ); |
| 44 | + |
| 45 | + $this->assertNotFalse( $translations->find( null, 'Not found' ) ); |
| 46 | + $this->assertNotFalse( $translations->find( null, 'Try again' ) ); |
| 47 | + } |
| 48 | + |
| 49 | + public function test_extracts_bound_props_from_multiline_component_tag() { |
| 50 | + $blade = <<<'BLADE' |
| 51 | +<x-no-results |
| 52 | + :title="__('Page not found', 'foo-theme')" |
| 53 | + :subtitle="esc_html__('Please try again', 'foo-theme')" |
| 54 | +/> |
| 55 | +BLADE; |
| 56 | + |
| 57 | + $translations = $this->extract( $blade ); |
| 58 | + |
| 59 | + $this->assertNotFalse( $translations->find( null, 'Page not found' ) ); |
| 60 | + $this->assertNotFalse( $translations->find( null, 'Please try again' ) ); |
| 61 | + } |
| 62 | + |
| 63 | + public function test_extracts_bound_props_from_open_component_tag() { |
| 64 | + $blade = <<<'BLADE' |
| 65 | +<x-alert :message="__('Warning message', 'foo-theme')"> |
| 66 | + {!! __('Content inside', 'foo-theme') !!} |
| 67 | +</x-alert> |
| 68 | +BLADE; |
| 69 | + |
| 70 | + $translations = $this->extract( $blade ); |
| 71 | + |
| 72 | + $this->assertNotFalse( $translations->find( null, 'Warning message' ) ); |
| 73 | + $this->assertNotFalse( $translations->find( null, 'Content inside' ) ); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_ignores_static_props() { |
| 77 | + $translations = $this->extract( |
| 78 | + '<x-alert type="warning" :message="__(\'Hello\', \'foo-theme\')" />' |
| 79 | + ); |
| 80 | + |
| 81 | + $this->assertNotFalse( $translations->find( null, 'Hello' ) ); |
| 82 | + $this->assertFalse( $translations->find( null, 'warning' ) ); |
| 83 | + } |
| 84 | + |
| 85 | + public function test_does_not_match_non_component_html() { |
| 86 | + $translations = $this->extract( |
| 87 | + '<a href="https://example.com">{{ __(\'Link text\', \'foo-theme\') }}</a>' |
| 88 | + ); |
| 89 | + |
| 90 | + $this->assertNotFalse( $translations->find( null, 'Link text' ) ); |
| 91 | + // Only 1 translation should exist. |
| 92 | + $this->assertCount( 1, $translations ); |
| 93 | + } |
| 94 | + |
| 95 | + public function test_extracts_context_function_in_prop() { |
| 96 | + $translations = $this->extract( |
| 97 | + '<x-button :label="_x(\'Read\', \'verb\', \'foo-theme\')" />' |
| 98 | + ); |
| 99 | + |
| 100 | + $translation = $translations->find( 'verb', 'Read' ); |
| 101 | + $this->assertNotFalse( $translation ); |
| 102 | + } |
| 103 | + |
| 104 | + public function test_extracts_esc_functions_in_props() { |
| 105 | + $blade = <<<'BLADE' |
| 106 | +<x-field |
| 107 | + :label="esc_html__('Username', 'foo-theme')" |
| 108 | + :placeholder="esc_attr__('Enter username', 'foo-theme')" |
| 109 | +/> |
| 110 | +BLADE; |
| 111 | + |
| 112 | + $translations = $this->extract( $blade ); |
| 113 | + |
| 114 | + $this->assertNotFalse( $translations->find( null, 'Username' ) ); |
| 115 | + $this->assertNotFalse( $translations->find( null, 'Enter username' ) ); |
| 116 | + } |
| 117 | + |
| 118 | + public function test_extracts_single_quoted_bound_props() { |
| 119 | + $translations = $this->extract( |
| 120 | + "<x-alert :message='__(\"Single quoted\", \"foo-theme\")' />" |
| 121 | + ); |
| 122 | + |
| 123 | + $this->assertNotFalse( $translations->find( null, 'Single quoted' ) ); |
| 124 | + } |
| 125 | + |
| 126 | + public function test_existing_blade_extraction_still_works() { |
| 127 | + $blade = <<<'BLADE' |
| 128 | +@php |
| 129 | + __('PHP block string', 'foo-theme'); |
| 130 | +@endphp |
| 131 | +{{ __('Echo string', 'foo-theme') }} |
| 132 | +{!! __('Raw string', 'foo-theme') !!} |
| 133 | +@php(__('Directive string', 'foo-theme')) |
| 134 | +BLADE; |
| 135 | + |
| 136 | + $translations = $this->extract( $blade ); |
| 137 | + |
| 138 | + $this->assertNotFalse( $translations->find( null, 'PHP block string' ) ); |
| 139 | + $this->assertNotFalse( $translations->find( null, 'Echo string' ) ); |
| 140 | + $this->assertNotFalse( $translations->find( null, 'Raw string' ) ); |
| 141 | + $this->assertNotFalse( $translations->find( null, 'Directive string' ) ); |
| 142 | + } |
| 143 | +} |
0 commit comments