@@ -15,133 +15,144 @@ class Test_Attachment_Db_Renamer extends WP_UnitTestCase {
15
15
*/
16
16
private $ attachment_id ;
17
17
18
+ /**
19
+ * @var Optml_Attachment_Db_Renamer
20
+ */
21
+ private $ replacer ;
22
+
23
+ private $ replace_method ;
24
+
18
25
/**
19
26
* Setup test
20
27
*/
21
28
public function setUp (): void {
22
29
parent ::setUp ();
23
- $ this ->attachment_id = $ this ->create_attachment_get_id (OPTML_PATH . 'tests/assets/sample-test.jpg ' );
30
+ $ this ->attachment_id = $ this ->create_attachment_get_id ( OPTML_PATH . 'tests/assets/sample-test.jpg ' );
31
+ $ this ->replacer = new Optml_Attachment_Db_Renamer ();
32
+
33
+ $ this ->replace_method = new ReflectionMethod ( 'Optml_Attachment_Db_Renamer ' , 'replace_urls_in_value ' );
34
+ $ this ->replace_method ->setAccessible ( true );
24
35
}
25
36
26
37
/**
27
38
* Clean up after each test
28
39
*/
29
40
public function tearDown (): void {
30
- if ($ this ->attachment_id ) {
31
- $ this ->delete_attachment ($ this ->attachment_id );
41
+ if ( $ this ->attachment_id ) {
42
+ $ this ->delete_attachment ( $ this ->attachment_id );
32
43
}
33
44
parent ::tearDown ();
34
45
}
35
46
36
47
/**
37
- * Test basic URL replacement
48
+ * Test replace method with valid URLs
38
49
*/
39
- public function test_basic_url_replacement () {
40
- $ attachment = new Optml_Attachment_Model ($ this ->attachment_id );
41
- $ old_url = $ attachment ->get_main_url ();
42
- $ new_url = str_replace ('sample-test ' , 'new-test-image ' , $ old_url );
50
+ public function test_replace () {
51
+ $ attachment = new Optml_Attachment_Model ( $ this ->attachment_id );
52
+ $ old_url = $ attachment ->get_main_url ();
53
+ $ new_url = str_replace ( 'sample-test ' , 'new-test-image ' , $ old_url );
43
54
44
- $ replacer = new Optml_Attachment_Db_Renamer ();
45
- $ count = $ replacer ->replace ($ old_url , $ new_url );
46
-
47
- $ this ->assertGreaterThan (0 , $ count );
55
+ $ count = $ this ->replacer ->replace ( $ old_url , $ new_url );
56
+ $ this ->assertGreaterThan ( 0 , $ count );
48
57
}
49
58
50
59
/**
51
- * Test replacement with same URLs
60
+ * Test replace method with identical URLs
52
61
*/
53
- public function test_same_url_replacement () {
54
- $ attachment = new Optml_Attachment_Model ($ this ->attachment_id );
55
- $ url = $ attachment ->get_main_url ();
56
-
57
- $ replacer = new Optml_Attachment_Db_Renamer ();
58
- $ count = $ replacer ->replace ($ url , $ url );
62
+ public function test_replace_with_identical_urls () {
63
+ $ attachment = new Optml_Attachment_Model ( $ this ->attachment_id );
64
+ $ url = $ attachment ->get_main_url ();
59
65
60
- $ this ->assertEquals (0 , $ count );
66
+ $ count = $ this ->replacer ->replace ( $ url , $ url );
67
+ $ this ->assertEquals ( 0 , $ count );
61
68
}
62
69
63
70
/**
64
- * Test replacement with image sizes
71
+ * Test replace method with empty URLs
65
72
*/
66
- // public function test_image_sizes_replacement() {
67
- // $attachment = new Optml_Attachment_Model($this->attachment_id);
68
- // $old_url = $attachment->get_guid();
69
- // $new_url = str_replace('sample-test', 'new-test-image', $old_url);
70
- //
71
- // // Create a post with image sizes
72
- // $post_id = $this->factory->post->create();
73
- // $content = sprintf(
74
- // '<img src="%s" class="wp-image-%d" />',
75
- // $old_url,
76
- // $this->attachment_id
77
- // );
78
- // wp_update_post(['ID' => $post_id, 'post_content' => $content]);
79
- //
80
- // $replacer = new Optml_Attachment_Db_Renamer();
81
- // $count = $replacer->replace($old_url, $new_url);
82
- //
83
- // $this->assertGreaterThan(0, $count);
84
- //
85
- // $updated_post = get_post($post_id);
86
- // $this->assertStringContainsString($new_url, $updated_post->post_content);
87
- // }
73
+ public function test_replace_with_empty_urls () {
74
+ $ count = $ this ->replacer ->replace ( '' , '' );
75
+ $ this ->assertEquals ( 0 , $ count );
88
76
89
- /**
90
- * Test replacement with scaled images
91
- */
92
- // public function test_scaled_image_replacement() {
93
- // $scaled_attachment = $this->create_attachment_get_id(OPTML_PATH . 'tests/assets/3000x3000.jpg');
94
- // $attachment = new Optml_Attachment_Model($scaled_attachment);
95
- // $old_url = $attachment->get_guid();
96
- // $new_url = str_replace('3000x3000', 'new-scaled-image', $old_url);
97
- //
98
- // // Create a post with scaled image
99
- // $post_id = $this->factory->post->create();
100
- // $content = sprintf(
101
- // '<img src="%s" class="wp-image-%d" />',
102
- // $old_url,
103
- // $scaled_attachment
104
- // );
105
- // wp_update_post(['ID' => $post_id, 'post_content' => $content]);
106
- //
107
- // $replacer = new Optml_Attachment_Db_Renamer();
108
- // $count = $replacer->replace($old_url, $new_url);
109
- //
110
- // $this->assertGreaterThan(0, $count);
111
- //
112
- // $updated_post = get_post($post_id);
113
- // $this->assertStringContainsString($new_url, $updated_post->post_content);
114
- //
115
- // $this->delete_attachment($scaled_attachment);
116
- // }
77
+ $ count = $ this ->replacer ->replace ( 'http://example.com ' , '' );
78
+ $ this ->assertEquals ( 0 , $ count );
79
+
80
+ $ count = $ this ->replacer ->replace ( '' , 'http://example.com ' );
81
+ $ this ->assertEquals ( 0 , $ count );
82
+ }
117
83
118
84
/**
119
- * Test replacement with serialized data
85
+ * Test replace method with null URLs
120
86
*/
121
- // public function test_serialized_data_replacement() {
122
- // $attachment = new Optml_Attachment_Model($this->attachment_id);
123
- // $old_url = $attachment->get_guid();
124
- // $new_url = str_replace('sample-test', 'new-test-image', $old_url);
125
- //
126
- // // Create a post with serialized data
127
- // $post_id = $this->factory->post->create();
128
- // $meta_data = [
129
- // 'image_url' => $old_url,
130
- // 'sizes' => [
131
- // 'thumbnail' => str_replace('.jpg', '-150x150.jpg', $old_url),
132
- // 'medium' => str_replace('.jpg', '-300x300.jpg', $old_url)
133
- // ]
134
- // ];
135
- // update_post_meta($post_id, 'test_meta', $meta_data);
136
- //
137
- // $replacer = new Optml_Attachment_Db_Renamer();
138
- // $count = $replacer->replace($old_url, $new_url);
139
- //
140
- // $this->assertGreaterThan(0, $count);
141
- //
142
- // $updated_meta = get_post_meta($post_id, 'test_meta', true);
143
- // $this->assertEquals($new_url, $updated_meta['image_url']);
144
- // $this->assertStringContainsString($new_url, $updated_meta['sizes']['thumbnail']);
145
- // $this->assertStringContainsString($new_url, $updated_meta['sizes']['medium']);
146
- // }
87
+ public function test_replace_with_null_urls () {
88
+ $ count = $ this ->replacer ->replace ( null , null );
89
+ $ this ->assertEquals ( 0 , $ count );
90
+
91
+ $ count = $ this ->replacer ->replace ( 'http://example.com ' , null );
92
+ $ this ->assertEquals ( 0 , $ count );
93
+
94
+ $ count = $ this ->replacer ->replace ( null , 'http://example.com ' );
95
+ $ this ->assertEquals ( 0 , $ count );
96
+ }
97
+
98
+ public function test_simple_replacement () {
99
+ $ value = 'http://example.com ' ;
100
+ $ old_url = 'http://example.com ' ;
101
+ $ new_url = 'http://example.org ' ;
102
+
103
+ $ replaced = $ this ->replace_method ->invoke ( $ this ->replacer , $ value , $ old_url , $ new_url );
104
+ $ this ->assertEquals ( $ new_url , $ replaced );
105
+ }
106
+
107
+ public function test_multiple_replacement () {
108
+ $ value = 'http://example.com http://example.com http://example.com ' ;
109
+ $ old_url = 'http://example.com ' ;
110
+ $ new_url = 'http://example.org ' ;
111
+
112
+ $ replaced = $ this ->replace_method ->invoke ( $ this ->replacer , $ value , $ old_url , $ new_url );
113
+ $ this ->assertStringNotContainsString ( $ old_url , $ replaced );
114
+ $ this ->assertStringContainsString ( $ new_url , $ replaced );
115
+ }
116
+
117
+ public function test_replacing_scaled_urls () {
118
+ $ value = "<img src= \"http://example.com/wp-content/uploads/2020/01/image-150x150.jpg \" /><img src= \"http://example.com/wp-content/uploads/2020/01/image-300x300.jpg \" /><img src= \"http://example.com/wp-content/uploads/2020/01/image-scaled.jpg \" /><img src= \"http://example.com/wp-content/uploads/2020/01/image.jpg \" /> " ;
119
+
120
+ $ old_url = 'http://example.com/wp-content/uploads/2020/01/image.jpg ' ;
121
+ $ new_url = 'http://example.com/wp-content/uploads/2020/01/new-url.jpg ' ;
122
+
123
+ $ replaced = $ this ->replace_method ->invoke ( $ this ->replacer , $ value , $ old_url , $ new_url );
124
+
125
+ $ this ->assertStringNotContainsString ( $ old_url , $ replaced );
126
+ $ this ->assertStringContainsString ( $ new_url , $ replaced );
127
+
128
+ $ this ->assertStringContainsString ( 'http://example.com/wp-content/uploads/2020/01/new-url-150x150.jpg ' , $ replaced );
129
+ $ this ->assertStringContainsString ( 'http://example.com/wp-content/uploads/2020/01/new-url-300x300.jpg ' , $ replaced );
130
+ $ this ->assertStringContainsString ( 'http://example.com/wp-content/uploads/2020/01/new-url-scaled.jpg ' , $ replaced );
131
+ }
132
+
133
+ public function test_replacing_serialized_content () {
134
+ $ value = 'a:2:{s:5:"image";s:63:"http://example.com/wp-content/uploads/2020/01/image.jpg";s:5:"thumb";s:63:"http://example.com/wp-content/uploads/2020/01/thumb.jpg";} ' ;
135
+
136
+ $ old_url = 'http://example.com/wp-content/uploads/2020/01/image.jpg ' ;
137
+ $ new_url = 'http://example.com/wp-content/uploads/2020/01/new-url.jpg ' ;
138
+
139
+ $ replaced = $ this ->replace_method ->invoke ( $ this ->replacer , $ value , $ old_url , $ new_url );
140
+
141
+ $ this ->assertStringNotContainsString ( $ old_url , $ replaced );
142
+ $ this ->assertStringContainsString ( $ new_url , $ replaced );
143
+ $ this ->assertStringContainsString ( 'thumb.jpg ' , $ replaced );
144
+ }
145
+
146
+ public function test_replacing_json_content () {
147
+ $ value = '{"image":"http://example.com/wp-content/uploads/2020/01/image.jpg","thumb":"http://example.com/wp-content/uploads/2020/01/thumb.jpg"} ' ;
148
+
149
+ $ old_url = 'http://example.com/wp-content/uploads/2020/01/image.jpg ' ;
150
+ $ new_url = 'http://example.com/wp-content/uploads/2020/01/new-url.jpg ' ;
151
+
152
+ $ replaced = $ this ->replace_method ->invoke ( $ this ->replacer , $ value , $ old_url , $ new_url );
153
+
154
+ $ this ->assertStringNotContainsString ( $ old_url , $ replaced );
155
+ $ this ->assertStringContainsString ( $ new_url , $ replaced );
156
+ $ this ->assertStringContainsString ( 'thumb.jpg ' , $ replaced );
157
+ }
147
158
}
0 commit comments