|
| 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\Text\get_the_text; |
| 10 | +use function BEA\Theme\Framework\Helpers\Formatting\Text\the_text; |
| 11 | +use function ob_get_clean; |
| 12 | +use function ob_start; |
| 13 | + |
| 14 | +class TextTest extends TestCase { |
| 15 | + public function setUp(): void { |
| 16 | + WP_Mock::setUp(); |
| 17 | + } |
| 18 | + |
| 19 | + public function tearDown(): void { |
| 20 | + WP_Mock::tearDown(); |
| 21 | + } |
| 22 | + |
| 23 | + public function testEmpty() { |
| 24 | + $test = get_the_text( '' ); |
| 25 | + $this->assertEmpty( $test ); |
| 26 | + |
| 27 | + ob_start(); |
| 28 | + the_text( '' ); |
| 29 | + $buffer = ob_get_clean(); |
| 30 | + $this->assertSame( $test, $buffer ); |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + public function testBeforeAfter() { |
| 35 | + WP_Mock::userFunction( 'wp_parse_args', [ |
| 36 | + 'times' => 6, |
| 37 | + 'return_in_order' => [ |
| 38 | + [ |
| 39 | + 'before' => 'b', |
| 40 | + 'after' => '', |
| 41 | + 'escape' => '', |
| 42 | + ], |
| 43 | + [ |
| 44 | + 'before' => '', |
| 45 | + 'after' => 'a', |
| 46 | + 'escape' => '', |
| 47 | + ], |
| 48 | + [ |
| 49 | + 'before' => 'b', |
| 50 | + 'after' => 'a', |
| 51 | + 'escape' => '', |
| 52 | + ], |
| 53 | + [ |
| 54 | + 'before' => 'b', |
| 55 | + 'after' => '', |
| 56 | + 'escape' => '', |
| 57 | + ], |
| 58 | + [ |
| 59 | + 'before' => '', |
| 60 | + 'after' => 'a', |
| 61 | + 'escape' => '', |
| 62 | + ], |
| 63 | + [ |
| 64 | + 'before' => 'b', |
| 65 | + 'after' => 'a', |
| 66 | + 'escape' => '', |
| 67 | + ], |
| 68 | + ], |
| 69 | + ] ); |
| 70 | + |
| 71 | + $val = get_the_text( 'val', [ 'before' => 'b' ] ); |
| 72 | + $val2 = get_the_text( 'val', [ 'after' => 'a' ] ); |
| 73 | + $val3 = get_the_text( 'val', [ 'before' => 'b', 'after' => 'a' ] ); |
| 74 | + |
| 75 | + ob_start(); |
| 76 | + the_text( 'val', [ 'before' => 'b' ] ); |
| 77 | + $buffer = ob_get_clean(); |
| 78 | + |
| 79 | + ob_start(); |
| 80 | + the_text( 'val', [ 'after' => 'a' ] ); |
| 81 | + $buffer2 = ob_get_clean(); |
| 82 | + |
| 83 | + ob_start(); |
| 84 | + the_text( 'val', [ 'before' => 'b', 'after' => 'a' ] ); |
| 85 | + $buffer3 = ob_get_clean(); |
| 86 | + |
| 87 | + // Test returns |
| 88 | + $this->assertSame( 'bval', $val ); |
| 89 | + $this->assertSame( 'vala', $val2 ); |
| 90 | + $this->assertSame( 'bvala', $val3 ); |
| 91 | + |
| 92 | + // Test Buffer and generated the same |
| 93 | + $this->assertSame( $val, $buffer ); |
| 94 | + $this->assertSame( $val2, $buffer2 ); |
| 95 | + $this->assertSame( $val3, $buffer3 ); |
| 96 | + } |
| 97 | + |
| 98 | + public function testEscape() { |
| 99 | + WP_Mock::userFunction( 'wp_parse_args', [ |
| 100 | + 'times' => 4, |
| 101 | + 'return_in_order' => [ |
| 102 | + [ |
| 103 | + 'before' => '', |
| 104 | + 'after' => '', |
| 105 | + 'escape' => 'test_escape', |
| 106 | + ], |
| 107 | + [ |
| 108 | + 'before' => 'b', |
| 109 | + 'after' => '', |
| 110 | + 'escape' => 'test_escape', |
| 111 | + ], |
| 112 | + [ |
| 113 | + 'before' => '', |
| 114 | + 'after' => 'a', |
| 115 | + 'escape' => 'test_escape', |
| 116 | + ], |
| 117 | + [ |
| 118 | + 'before' => 'b', |
| 119 | + 'after' => 'a', |
| 120 | + 'escape' => 'test_escape', |
| 121 | + ], |
| 122 | + ], |
| 123 | + ] ); |
| 124 | + |
| 125 | + $this->assertSame( 'esc_val', get_the_text( 'val', [ |
| 126 | + 'before' => '', |
| 127 | + 'after' => '', |
| 128 | + 'escape' => 'test_escape', |
| 129 | + ] ) ); |
| 130 | + $this->assertSame( 'besc_val', get_the_text( 'val', [ |
| 131 | + 'before' => 'b', |
| 132 | + 'after' => '', |
| 133 | + 'escape' => 'test_escape', |
| 134 | + ] ) ); |
| 135 | + $this->assertSame( 'esc_vala', get_the_text( 'val', [ |
| 136 | + 'before' => '', |
| 137 | + 'after' => 'a', |
| 138 | + 'escape' => 'test_escape', |
| 139 | + ] ) ); |
| 140 | + $this->assertSame( 'besc_vala', get_the_text( 'val', [ |
| 141 | + 'before' => 'b', |
| 142 | + 'after' => 'a', |
| 143 | + 'escape' => 'test_escape', |
| 144 | + ] ) ); |
| 145 | + } |
| 146 | + |
| 147 | + public function testFilterSettings() { |
| 148 | + WP_Mock::userFunction( 'wp_parse_args', [ |
| 149 | + 'return' => |
| 150 | + [ |
| 151 | + 'before' => '', |
| 152 | + 'after' => '', |
| 153 | + 'escape' => 'test_escape', |
| 154 | + ], |
| 155 | + ] ); |
| 156 | + |
| 157 | + WP_Mock::onFilter( 'bea_theme_framework_text_settings' )->with( [ |
| 158 | + 'before' => '', |
| 159 | + 'after' => '', |
| 160 | + 'escape' => 'test_escape', |
| 161 | + ], 'val' )->reply( [ |
| 162 | + 'before' => 'b', |
| 163 | + 'after' => 'a', |
| 164 | + 'escape' => 'esc_html', |
| 165 | + ] ); |
| 166 | + |
| 167 | + $this->assertSame( 'bvala', get_the_text( 'val' ) ); |
| 168 | + } |
| 169 | + |
| 170 | + public function testFilterValue() { |
| 171 | + WP_Mock::userFunction( 'wp_parse_args', [ |
| 172 | + 'return' => |
| 173 | + [ |
| 174 | + 'before' => '', |
| 175 | + 'after' => '', |
| 176 | + 'escape' => 'test_escape', |
| 177 | + ], |
| 178 | + ] ); |
| 179 | + |
| 180 | + WP_Mock::onFilter( 'bea_theme_framework_text_value' )->with( 'esc_val', [ |
| 181 | + 'before' => '', |
| 182 | + 'after' => '', |
| 183 | + 'escape' => 'test_escape', |
| 184 | + ] )->reply( 'filtered' ); |
| 185 | + |
| 186 | + $this->assertSame( 'filtered', get_the_text( 'val' ) ); |
| 187 | + } |
| 188 | +} |
| 189 | + |
0 commit comments