Skip to content

Commit b144b2e

Browse files
authored
Merge pull request #324 from lloc/refactoring-version-2-8
Refactoring version 2 8
2 parents 5e5e874 + 6d68d6e commit b144b2e

12 files changed

+92
-46
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ phpunit.xml.bak
77
tests/coverage/
88
tests/playwright-results/
99
tests/playwright-report/
10+
tests/playwright/.env.local
11+
tests/playwright/.auth/
1012
node_modules/
1113
vendor/
12-
/blob-report/
13-
/playwright/.cache/
14+

includes/ContentImport/ContentImporter.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function set_relations( $relations ) {
8888
*
8989
* @return array The updated, if needed, data array.
9090
*/
91-
public function handle_import( array $data = [] ) {
91+
public function handle_import( array $data = array() ) {
9292
if ( ! $this->pre_flight_check() || false === $sources = $this->parse_sources() ) {
9393
return $data;
9494
}
@@ -144,7 +144,7 @@ public function handle_import( array $data = [] ) {
144144
*
145145
* @return bool
146146
*/
147-
protected function pre_flight_check( array $data = [] ) {
147+
protected function pre_flight_check( array $data = array() ) {
148148
if ( ! $this->handle ) {
149149
return false;
150150
}
@@ -194,15 +194,15 @@ protected function get_the_blog_post_ID( $blog_id ) {
194194
return (int) $_REQUEST['post'];
195195
}
196196

197-
$data = [
197+
$data = array(
198198
'post_type' => $this->read_post_type_from_request( 'post' ),
199-
'post_title' => 'MSLS Content Import Draft - ' . date( 'Y-m-d H:i:s' )
200-
];
199+
'post_title' => 'MSLS Content Import Draft - ' . ( new \DateTimeImmutable() )->format( 'Y-m-d H:i:s' ),
200+
);
201201

202202
return $this->insert_blog_post( $blog_id, $data );
203203
}
204204

205-
protected function insert_blog_post( $blog_id, array $data = [] ) {
205+
protected function insert_blog_post( $blog_id, array $data = array() ) {
206206
if ( empty( $data ) ) {
207207
return false;
208208
}
@@ -239,13 +239,13 @@ public function handle( $handle ) {
239239
* Imports content according to the provided coordinates.
240240
*
241241
* @param ImportCoordinates $import_coordinates
242-
* @param array $post_fields An optional array of post fields; this can be
243-
* left empty if the method is not called as a consequence
244-
* of filtering the `wp_insert_post_data` filter.
242+
* @param array $post_fields An optional array of post fields; this can be
243+
* left empty if the method is not called as a consequence
244+
* of filtering the `wp_insert_post_data` filter.
245245
*
246246
* @return array An array of modified post fields.
247247
*/
248-
public function import_content( ImportCoordinates $import_coordinates, array $post_fields = [] ) {
248+
public function import_content( ImportCoordinates $import_coordinates, array $post_fields = array() ) {
249249
if ( ! $import_coordinates->validate() ) {
250250
return $post_fields;
251251
}
@@ -264,7 +264,6 @@ public function import_content( ImportCoordinates $import_coordinates, array $po
264264
* @param ImportCoordinates $import_coordinates
265265
*
266266
* @since TBD
267-
*
268267
*/
269268
$post_fields = apply_filters( 'msls_content_import_data_before_import', $post_fields, $import_coordinates );
270269

@@ -311,7 +310,6 @@ public function import_content( ImportCoordinates $import_coordinates, array $po
311310
* @param Relations $relations
312311
*
313312
* @since TBD
314-
*
315313
*/
316314
do_action( 'msls_content_import_after_import', $import_coordinates, $this->logger, $this->relations );
317315

@@ -323,16 +321,18 @@ public function import_content( ImportCoordinates $import_coordinates, array $po
323321
* @param ImportLogger $logger
324322
* @param Relations $relations
325323
*/
326-
return apply_filters( 'msls_content_import_data_after_import',
324+
return apply_filters(
325+
'msls_content_import_data_after_import',
327326
$post_fields,
328327
$import_coordinates,
329328
$this->logger,
330-
$this->relations );
329+
$this->relations
330+
);
331331
}
332332

333333
/**
334334
* @param array $data
335-
* @param int $post_id
335+
* @param int $post_id
336336
*
337337
* @return array
338338
*/

includes/ContentImport/ImportLogger.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
namespace lloc\Msls\ContentImport;
44

5-
65
use lloc\Msls\ContentImport\LogWriters\AdminNoticeLogger;
76
use lloc\Msls\ContentImport\LogWriters\LogWriter;
87

98
class ImportLogger {
109

1110
protected $levels_delimiter = '/';
1211

13-
protected $data = [
14-
'info' => [],
15-
'error' => [],
16-
'success' => [],
17-
];
12+
protected $data = array(
13+
'info' => array(),
14+
'error' => array(),
15+
'success' => array(),
16+
);
1817

1918
/**
2019
* @var ImportCoordinates
@@ -66,7 +65,7 @@ public function save() {
6665
}
6766

6867
if ( is_string( $log_writer ) ) {
69-
$log_writer = new $log_writer;
68+
$log_writer = new $log_writer();
7069
}
7170

7271
if ( ! $log_writer instanceof LogWriter ) {
@@ -81,7 +80,7 @@ public function save() {
8180
* Logs an error.
8281
*
8382
* @param string $where A location string using `/` as level format.
84-
* @param mixed $what What should be stored in the log.
83+
* @param mixed $what What should be stored in the log.
8584
*/
8685
public function log_error( $where, $what ) {
8786
$this->log( $where, $what, 'error' );
@@ -91,30 +90,31 @@ public function log_error( $where, $what ) {
9190
* Logs something.
9291
*
9392
* @param string $where A location string using `/` as level format.
94-
* @param mixed $what What should be stored in the log.
93+
* @param mixed $what What should be stored in the log.
9594
* @param string $root Where to log the information.
9695
*/
9796
protected function log( $where, $what, $root = 'info' ) {
9897
if ( ! isset( $this->data[ $root ] ) ) {
99-
$this->data[ $root ] = [];
98+
$this->data[ $root ] = array();
10099
}
101100

102101
$data = $this->build_nested_array( $this->build_path( $where ), $what );
103102

104-
105103
$this->data[ $root ] = array_merge_recursive( $this->data[ $root ], $data );
106104
}
107105

108106
protected function build_nested_array( $path, $what = '' ) {
109107
$json = '{"'
110-
. implode( '":{"', $path )
111-
. '":' . json_encode( $what )
112-
. implode(
113-
'',
114-
array_fill( 0,
115-
count( $path ),
116-
'}' )
117-
);
108+
. implode( '":{"', $path )
109+
. '":' . wp_json_encode( $what )
110+
. implode(
111+
'',
112+
array_fill(
113+
0,
114+
count( $path ),
115+
'}'
116+
)
117+
);
118118
$data = json_decode( $json, true );
119119

120120
return $data;
@@ -153,7 +153,7 @@ public function set_levels_delimiter( $levels_delimiter ) {
153153
* Logs a success.
154154
*
155155
* @param string $where A location string using `/` as level format.
156-
* @param mixed $what What should be stored in the log.
156+
* @param mixed $what What should be stored in the log.
157157
*/
158158
public function log_success( $where, $what ) {
159159
$this->log( $where, $what, 'success' );
@@ -196,4 +196,4 @@ public function get_success( $where ) {
196196
public function get_information( $key ) {
197197
return isset( $this->data['info'][ $key ] ) ? $this->data['info'][ $key ] : '';
198198
}
199-
}
199+
}

includes/MslsJson.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function add( $value, $label ) {
3737
/**
3838
* Compare the item with the key "label" of the array $a and the array $b
3939
*
40-
* @param array $a
41-
* @param array $b
40+
* @param array<string, string> $a
41+
* @param array<string, string> $b
4242
*
4343
* @return int
4444
*/
@@ -65,7 +65,7 @@ public function get(): array {
6565
* @return string
6666
*/
6767
public function encode(): string {
68-
return json_encode( $this->get() );
68+
return wp_json_encode( $this->get() );
6969
}
7070

7171
/**

includes/MslsWidget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function update( $new_instance, $old_instance ) {
8383
$instance = $old_instance;
8484

8585
if ( isset( $new_instance['title'] ) ) {
86-
$instance['title'] = strip_tags( $new_instance['title'] );
86+
$instance['title'] = wp_strip_all_tags( $new_instance['title'] );
8787
}
8888

8989
return $instance;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"devDependencies": {
1414
"@playwright/test": "^1.44.0",
15+
"@types/node": "^20.12.11",
1516
"@wordpress/scripts": "^27.9.0",
16-
"@types/node": "^20.12.11"
17+
"dotenv": "^16.4.5"
1718
}
1819
}

playwright.config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ export default defineConfig({
1515
trace: 'on-first-retry',
1616
},
1717
projects: [
18+
{
19+
name: 'setup',
20+
testMatch: 'tests/playwright/auth.setup.ts',
21+
},
1822
{
1923
name: 'chromium',
20-
use: { ...devices['Desktop Chrome'] },
24+
use: {
25+
...devices['Desktop Chrome'],
26+
storageState: 'tests/playwright/.auth/user.json',
27+
},
28+
dependencies: ['setup'],
2129
},
2230
],
2331
});

tests/phpunit/TestMslsWidget.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function test_widget(): void {
4040
}
4141

4242
public function test_update(): void {
43+
Functions\expect( 'wp_strip_all_tags' )->twice()->andReturnFirstArg();
44+
4345
$result = $this->test->update( array(), array() );
4446
$this->assertEquals( array(), $result );
4547

tests/playwright/.env.local.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
user_login=set_a_username
2+
user_pass=set_a_password
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { test, expect } from '@playwright/test';
2+
3+
test.describe.configure({ mode: 'parallel' });
4+
test('test edit posts', async ({ page }) => {
5+
await page.goto('https://msls.co/wp-admin/edit.php');
6+
7+
const mslscol = await page.locator('#mslscol');
8+
await expect(mslscol).toHaveAttribute('scope', 'col');
9+
});
10+
11+
test('test edit pages', async ({ page }) => {
12+
await page.goto('https://msls.co/wp-admin/edit.php?post_type=page');
13+
14+
const mslscol = await page.locator('#mslscol');
15+
await expect(mslscol).toHaveAttribute('scope', 'col');
16+
});

tests/playwright/auth.setup.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { test as setup, expect } from '@playwright/test';
2+
import dotenv from 'dotenv';
3+
4+
const authFile = 'tests/playwright/.auth/user.json';
5+
6+
setup('authenticate', async ({ page }) => {
7+
dotenv.config({ path: 'tests/playwright/.env.local'});
8+
9+
await page.goto('https://msls.co/wp-login.php?jetpack-sso-show-default-form=1');
10+
11+
await page.locator('#user_login').fill(process.env.user_login as string);
12+
await page.locator('#user_pass').fill(process.env.user_pass as string);
13+
await page.locator('#wp-submit').click();
14+
15+
await page.context().storageState({ path: authFile });
16+
});

tests/playwright/start.spec.ts renamed to tests/playwright/homepage.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ test('test', async ({ page }) => {
44
await page.goto('https://msls.co/');
55

66
await expect(page).toHaveTitle(/Multisite Language Switcher - WordPress multilingual/);
7-
await page.getByRole('link', { name: 'de_DE' }).click();
87

8+
await page.getByRole('link', { name: 'de_DE' }).click();
99
await expect(page).toHaveTitle(/Multisite Language Switcher - WordPress mehrsprachig/);
10-
await page.getByRole('link', { name: 'en_GB' }).click();
1110

11+
await page.getByRole('link', { name: 'en_GB' }).click();
1212
await expect(page).toHaveTitle(/Multisite Language Switcher - WordPress multilingual/);
1313
});

0 commit comments

Comments
 (0)