|
1 | 1 | import { PHP } from '@php-wasm/universal';
|
2 |
| -import { rewriteDefineCalls, defineBeforeRun } from './define-wp-config-consts'; |
| 2 | +import { defineBeforeRun } from './define-wp-config-consts'; |
3 | 3 | import { RecommendedPHPVersion } from '@wp-playground/common';
|
4 | 4 | import { loadNodeRuntime } from '@php-wasm/node';
|
5 | 5 | import { setupPlatformLevelMuPlugins } from '@wp-playground/wordpress';
|
6 | 6 |
|
7 |
| -describe('rewriteDefineCalls', () => { |
8 |
| - let php: PHP; |
9 |
| - beforeEach(async () => { |
10 |
| - php = new PHP(await loadNodeRuntime(RecommendedPHPVersion)); |
11 |
| - }); |
12 |
| - |
13 |
| - it('should print warnings when a constant name conflicts, just to make sure other tests would fail', async () => { |
14 |
| - const phpCode = `<?php |
15 |
| - define('SITE_URL','http://initial.value'); |
16 |
| - define('SITE_URL','http://initial.value'); |
17 |
| - `; |
18 |
| - const response = await php.run({ code: phpCode }); |
19 |
| - expect(response.errors).toContain('Constant SITE_URL already defined'); |
20 |
| - expect(response.text).toContain('Constant SITE_URL already defined'); |
21 |
| - }); |
22 |
| - |
23 |
| - it('should prepend constants not already present in the PHP code', async () => { |
24 |
| - const phpCode = `<?php |
25 |
| - echo json_encode([ |
26 |
| - "SITE_URL" => SITE_URL, |
27 |
| - ]); |
28 |
| - `; |
29 |
| - const rewritten = await rewriteDefineCalls(php, phpCode, { |
30 |
| - SITE_URL: 'http://test.url', |
31 |
| - }); |
32 |
| - expect(rewritten).toContain(`define('SITE_URL','http://test.url');`); |
33 |
| - |
34 |
| - const response = await php.run({ code: rewritten }); |
35 |
| - expect(response.errors).toHaveLength(0); |
36 |
| - expect(response.json).toEqual({ |
37 |
| - SITE_URL: 'http://test.url', |
38 |
| - }); |
39 |
| - }); |
40 |
| - |
41 |
| - it('should rewrite the define() calls for the constants that are already defined in the PHP code', async () => { |
42 |
| - const phpCode = `<?php |
43 |
| - define('SITE_URL','http://initial.value'); |
44 |
| - echo json_encode([ |
45 |
| - "SITE_URL" => SITE_URL, |
46 |
| - ]); |
47 |
| - `; |
48 |
| - const rewritten = await rewriteDefineCalls(php, phpCode, { |
49 |
| - SITE_URL: 'http://new.url', |
50 |
| - }); |
51 |
| - expect(rewritten).not.toContain( |
52 |
| - `define('SITE_URL','http://initial.value');` |
53 |
| - ); |
54 |
| - expect(rewritten).toContain(`define('SITE_URL','http://new.url');`); |
55 |
| - |
56 |
| - const response = await php.run({ code: rewritten }); |
57 |
| - expect(response.errors).toHaveLength(0); |
58 |
| - expect(response.json).toEqual({ |
59 |
| - SITE_URL: 'http://new.url', |
60 |
| - }); |
61 |
| - }); |
62 |
| - |
63 |
| - it('should preserve the third argument in existing define() calls', async () => { |
64 |
| - const phpCode = `<?php |
65 |
| - define('SITE_URL','http://initial.value',true); |
66 |
| - echo json_encode([ |
67 |
| - "SITE_URL" => SITE_URL, |
68 |
| - ]); |
69 |
| - `; |
70 |
| - const rewritten = await rewriteDefineCalls(php, phpCode, { |
71 |
| - SITE_URL: 'http://new.url', |
72 |
| - }); |
73 |
| - expect(rewritten).not.toContain( |
74 |
| - `define('SITE_URL','http://initial.value',true);` |
75 |
| - ); |
76 |
| - expect(rewritten).toContain( |
77 |
| - `define('SITE_URL','http://new.url',true);` |
78 |
| - ); |
79 |
| - |
80 |
| - const response = await php.run({ code: rewritten }); |
81 |
| - |
82 |
| - expect(response.errors).toContain( |
83 |
| - 'case-insensitive constants is no longer supported' |
84 |
| - ); |
85 |
| - expect(response.text).toContain(`{"SITE_URL":"http:\\/\\/new.url"}`); |
86 |
| - }); |
87 |
| - |
88 |
| - it('should take define() calls where the constant name cannot be statically inferred and wrap them in if(!defined()) checks', async () => { |
89 |
| - const phpCode = `<?php |
90 |
| - define('SITE'.'_URL','http://initial.value'); |
91 |
| - echo json_encode([ |
92 |
| - "SITE_URL" => SITE_URL, |
93 |
| - ]); |
94 |
| - `; |
95 |
| - const rewritten = await rewriteDefineCalls(php, phpCode, {}); |
96 |
| - expect(rewritten).toContain(`if(!defined('SITE'.'_URL'))`); |
97 |
| - expect(rewritten).toContain( |
98 |
| - `define('SITE'.'_URL','http://initial.value');` |
99 |
| - ); |
100 |
| - |
101 |
| - const response = await php.run({ code: rewritten }); |
102 |
| - expect(response.errors).toHaveLength(0); |
103 |
| - expect(response.json).toEqual({ |
104 |
| - SITE_URL: 'http://initial.value', |
105 |
| - }); |
106 |
| - }); |
107 |
| - |
108 |
| - it('should not wrap the existing define() calls in if(!defined()) guards twice', async () => { |
109 |
| - const phpCode = `<?php |
110 |
| - if(!defined('SITE'.'_URL')) { |
111 |
| - define('SITE'.'_URL','http://initial.value'); |
112 |
| - } |
113 |
| - echo json_encode([ |
114 |
| - "SITE_URL" => SITE_URL, |
115 |
| - ]); |
116 |
| - `; |
117 |
| - const rewritten = await rewriteDefineCalls(php, phpCode, {}); |
118 |
| - expect(rewritten).toEqual(phpCode); |
119 |
| - }); |
120 |
| - |
121 |
| - it('should not wrap the existing define() calls in if(!defined()) guards twice, even if the existing guard is formatted differently than the define() call', async () => { |
122 |
| - const phpCode = `<?php |
123 |
| - if ( ! defined( |
124 |
| - 'SITE' . |
125 |
| - '_URL' |
126 |
| - ) ) { |
127 |
| - define('SITE'.'_URL','http://initial.value'); |
128 |
| - } |
129 |
| - echo json_encode([ |
130 |
| - "SITE_URL" => SITE_URL, |
131 |
| - ]); |
132 |
| - `; |
133 |
| - const rewritten = await rewriteDefineCalls(php, phpCode, {}); |
134 |
| - expect(rewritten).toEqual(phpCode); |
135 |
| - }); |
136 |
| - |
137 |
| - it('should not create conflicts between pre-existing "dynamically" named constants and the newly defined ones', async () => { |
138 |
| - const phpCode = `<?php |
139 |
| - define('SITE'.'_URL','http://initial.value'); |
140 |
| - echo json_encode([ |
141 |
| - "SITE_URL" => SITE_URL, |
142 |
| - ]); |
143 |
| - `; |
144 |
| - const rewritten = await rewriteDefineCalls(php, phpCode, { |
145 |
| - SITE_URL: 'http://new.url', |
146 |
| - }); |
147 |
| - expect(rewritten).toContain(`if(!defined('SITE'.'_URL'))`); |
148 |
| - expect(rewritten).toContain( |
149 |
| - `define('SITE'.'_URL','http://initial.value');` |
150 |
| - ); |
151 |
| - expect(rewritten).toContain(`define('SITE_URL','http://new.url');`); |
152 |
| - |
153 |
| - const response = await php.run({ code: rewritten }); |
154 |
| - expect(response.errors).toHaveLength(0); |
155 |
| - expect(response.json).toEqual({ |
156 |
| - SITE_URL: 'http://new.url', |
157 |
| - }); |
158 |
| - }); |
159 |
| - |
160 |
| - it('should handle a complex scenario', async () => { |
161 |
| - const phpCode = `<?php |
162 |
| -define('WP_DEBUG', true); |
163 |
| -
|
164 |
| -// The third define() argument is also supported: |
165 |
| -@define('SAVEQUERIES', false, true); |
166 |
| -
|
167 |
| -// Expression |
168 |
| -define(true ? 'WP_DEBUG_LOG' : 'WP_DEBUG_LOG', 123); |
169 |
| -
|
170 |
| -// Guarded expressions shouldn't be wrapped twice |
171 |
| -if(!defined(1 ? 'A' : 'B')) { |
172 |
| - define(1 ? 'A' : 'B', 0); |
173 |
| -} |
174 |
| -
|
175 |
| -// More advanced expression |
176 |
| -$x = 'abc'; |
177 |
| -define((function() use($x) { |
178 |
| - return $x; |
179 |
| -})(), 123); |
180 |
| -echo json_encode([ |
181 |
| - "WP_DEBUG" => WP_DEBUG, |
182 |
| - "SAVEQUERIES" => SAVEQUERIES, |
183 |
| - "WP_DEBUG_LOG" => WP_DEBUG_LOG, |
184 |
| - "NEW_CONSTANT" => NEW_CONSTANT, |
185 |
| -]); |
186 |
| - `; |
187 |
| - const constants = { |
188 |
| - WP_DEBUG: false, |
189 |
| - WP_DEBUG_LOG: true, |
190 |
| - SAVEQUERIES: true, |
191 |
| - NEW_CONSTANT: 'new constant', |
192 |
| - }; |
193 |
| - const rewritten = await rewriteDefineCalls(php, phpCode, constants); |
194 |
| - const response = await php.run({ code: rewritten }); |
195 |
| - expect(response.errors).toHaveLength(0); |
196 |
| - expect(response.json).toEqual(constants); |
197 |
| - }); |
198 |
| -}); |
199 |
| - |
200 | 7 | describe('defineBeforeRun', () => {
|
201 | 8 | let php: PHP;
|
202 | 9 | beforeEach(async () => {
|
|
0 commit comments