1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace lloc\Msls\ContentImport\Importers\PostThumbnail; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use lloc\Msls\ContentImport\AttachmentPathFinder; |
7
|
|
|
use lloc\Msls\ContentImport\ImportCoordinates; |
8
|
|
|
use lloc\Msls\ContentImport\Service; |
9
|
|
|
use lloc\Msls\ContentImport\TestCase; |
10
|
|
|
|
11
|
|
|
class LinkingTest extends TestCase { |
12
|
|
|
/** |
13
|
|
|
* It should create an attachment for the post thumbnail in the destination blog |
14
|
|
|
* |
15
|
|
|
* @test |
16
|
|
|
*/ |
17
|
|
|
public function should_create_an_attachment_for_the_post_thumbnail_in_the_destination_blog() { |
18
|
|
|
list( $import_coordinates, $logger, $relations, $dest_post_data ) = $this->setup_source_and_dest(); |
19
|
|
|
/** @var ImportCoordinates $import_coordinates */ |
20
|
|
|
$dest_post_id = $import_coordinates->dest_post_id; |
21
|
|
|
|
22
|
|
|
switch_to_blog( $import_coordinates->source_blog_id ); |
23
|
|
|
$source_thumbnail_id = $this->factory()->attachment->create_upload_object( msls_test_data( 'images/image-one.jpg' ) ); |
24
|
|
|
set_post_thumbnail( $import_coordinates->source_post, $source_thumbnail_id ); |
25
|
|
|
|
26
|
|
|
// the import functionality should work starting fron any blog context |
27
|
|
|
restore_current_blog(); |
28
|
|
|
|
29
|
|
|
$obj = new Linking( $import_coordinates, $logger->reveal(), $relations->reveal() ); |
30
|
|
|
|
31
|
|
|
$mutated = $obj->import( $dest_post_data ); |
32
|
|
|
|
33
|
|
|
$this->assertEquals( $mutated, $dest_post_data ); |
34
|
|
|
|
35
|
|
|
switch_to_blog( $import_coordinates->dest_blog_id ); |
36
|
|
|
$attached_media = get_attached_media( 'image', $dest_post_id ); |
37
|
|
|
$this->assertCount( 1, $attached_media ); |
38
|
|
|
$post_thumbnail_id = array_keys( $attached_media )[0]; |
39
|
|
|
$this->assertEquals( get_post_thumbnail_id( $dest_post_id ), $post_thumbnail_id ); |
40
|
|
|
$this->assertEquals( [ |
41
|
|
|
'blog' => $import_coordinates->source_blog_id, |
42
|
|
|
'post' => $source_thumbnail_id, |
43
|
|
|
], get_post_meta( $post_thumbnail_id, AttachmentPathFinder::LINKED, true ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* It should replace the existing post thumbnail if already set |
48
|
|
|
* |
49
|
|
|
* @test |
50
|
|
|
*/ |
51
|
|
|
public function should_replace_the_existing_post_thumbnail_if_already_set() { |
52
|
|
|
list( $import_coordinates, $logger, $relations, $dest_post_data ) = $this->setup_source_and_dest(); |
53
|
|
|
/** @var ImportCoordinates $import_coordinates */ |
54
|
|
|
$dest_post_id = $import_coordinates->dest_post_id; |
55
|
|
|
|
56
|
|
|
switch_to_blog( $import_coordinates->source_blog_id ); |
57
|
|
|
$source_thumbnail_id = $this->factory()->attachment->create_upload_object( msls_test_data( 'images/image-one.jpg' ) ); |
58
|
|
|
set_post_thumbnail( $import_coordinates->source_post, $source_thumbnail_id ); |
59
|
|
|
|
60
|
|
|
switch_to_blog( $import_coordinates->dest_blog_id ); |
61
|
|
|
$existing_dest_post_thumbnail_id = $this->factory()->attachment->create_upload_object( msls_test_data( 'images/image-one.jpg' ) ); |
62
|
|
|
set_post_thumbnail( $dest_post_id, $existing_dest_post_thumbnail_id ); |
63
|
|
|
|
64
|
|
|
// the import functionality should work starting fron any blog context |
65
|
|
|
restore_current_blog(); |
66
|
|
|
|
67
|
|
|
$obj = new Linking( $import_coordinates, $logger->reveal(), $relations->reveal() ); |
68
|
|
|
|
69
|
|
|
$mutated = $obj->import( $dest_post_data ); |
70
|
|
|
|
71
|
|
|
$this->assertEquals( $mutated, $dest_post_data ); |
72
|
|
|
|
73
|
|
|
switch_to_blog( $import_coordinates->dest_blog_id ); |
74
|
|
|
$attached_media = get_attached_media( 'image', $dest_post_id ); |
75
|
|
|
$this->assertCount( 1, $attached_media ); |
76
|
|
|
$this->assertNotEquals( get_post_thumbnail_id( $dest_post_id ), $existing_dest_post_thumbnail_id ); |
77
|
|
|
$post_thumbnail_id = array_keys( $attached_media )[0]; |
78
|
|
|
$this->assertEquals( get_post_thumbnail_id( $dest_post_id ), $post_thumbnail_id ); |
79
|
|
|
$this->assertEquals( [ |
80
|
|
|
'blog' => $import_coordinates->source_blog_id, |
81
|
|
|
'post' => $source_thumbnail_id, |
82
|
|
|
], get_post_meta( $post_thumbnail_id, AttachmentPathFinder::LINKED, true ) ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* It should not re-create the post thumbnail attachment if already linked |
87
|
|
|
* |
88
|
|
|
* @test |
89
|
|
|
*/ |
90
|
|
|
public function should_not_re_create_the_post_thumbnail_attachment_if_already_linked() { |
91
|
|
|
list( $import_coordinates, $logger, $relations, $dest_post_data ) = $this->setup_source_and_dest(); |
92
|
|
|
/** @var ImportCoordinates $import_coordinates */ |
93
|
|
|
$dest_post_id = $import_coordinates->dest_post_id; |
94
|
|
|
|
95
|
|
|
switch_to_blog( $import_coordinates->source_blog_id ); |
96
|
|
|
$source_thumbnail_id = $this->factory()->attachment->create_upload_object( msls_test_data( 'images/image-one.jpg' ) ); |
97
|
|
|
set_post_thumbnail( $import_coordinates->source_post, $source_thumbnail_id ); |
98
|
|
|
|
99
|
|
|
// the import functionality should work starting fron any blog context |
100
|
|
|
restore_current_blog(); |
101
|
|
|
|
102
|
|
|
$obj = new Linking( $import_coordinates, $logger->reveal(), $relations->reveal() ); |
103
|
|
|
|
104
|
|
|
$obj->import( $dest_post_data ); |
105
|
|
|
|
106
|
|
|
switch_to_blog( $import_coordinates->dest_blog_id ); |
107
|
|
|
$post_thumbnail_id_after_first_import = get_post_thumbnail_id($dest_post_id); |
108
|
|
|
|
109
|
|
|
$obj->import( $dest_post_data ); |
110
|
|
|
|
111
|
|
|
switch_to_blog( $import_coordinates->dest_blog_id ); |
112
|
|
|
$this->assertEquals( $post_thumbnail_id_after_first_import, get_post_thumbnail_id( $dest_post_id ) ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* It should not duplicate the post thumbnail files |
117
|
|
|
* |
118
|
|
|
* @test |
119
|
|
|
*/ |
120
|
|
|
public function should_not_duplicate_the_post_thumbnail_files() { |
121
|
|
|
list( $import_coordinates, $logger, $relations, $dest_post_data ) = $this->setup_source_and_dest(); |
122
|
|
|
|
123
|
|
|
switch_to_blog( $import_coordinates->source_blog_id ); |
124
|
|
|
$source_thumbnail_id = $this->factory()->attachment->create_upload_object( msls_test_data( 'images/image-one.jpg' ) ); |
125
|
|
|
set_post_thumbnail( $import_coordinates->source_post, $source_thumbnail_id ); |
126
|
|
|
|
127
|
|
|
switch_to_blog( $import_coordinates->dest_blog_id ); |
128
|
|
|
$uploads_folder = wp_upload_dir(); |
129
|
|
|
// in tests the path might contain double `/`, a test issue |
130
|
|
|
$path = str_replace( '//', '/', $uploads_folder['path'] ); |
131
|
|
|
$this->assertEmpty( glob( $path . '/image-one*.jpg' ) ); |
132
|
|
|
|
133
|
|
|
// the import functionality should work starting fron any blog context |
134
|
|
|
restore_current_blog(); |
135
|
|
|
|
136
|
|
|
$obj = new Linking( $import_coordinates, $logger->reveal(), $relations->reveal() ); |
137
|
|
|
|
138
|
|
|
$obj->import( $dest_post_data ); |
139
|
|
|
|
140
|
|
|
switch_to_blog( $import_coordinates->dest_blog_id ); |
141
|
|
|
$uploads_folder = wp_upload_dir(); |
142
|
|
|
// in tests the path might contain double `/`, a test issue |
143
|
|
|
$path = str_replace( '//', '/', $uploads_folder['path'] ); |
144
|
|
|
$this->assertEmpty( glob( $path . '/image-one*.jpg' ) ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* It should return the source post attachment URLs in HTML |
149
|
|
|
* |
150
|
|
|
* @test |
151
|
|
|
*/ |
152
|
|
|
public function should_return_the_source_post_attachment_ur_ls_in_html() { |
153
|
|
|
Service::instance()->hook(); |
154
|
|
|
|
155
|
|
|
list( $import_coordinates, $logger, $relations, $dest_post_data ) = $this->setup_source_and_dest(); |
156
|
|
|
|
157
|
|
|
switch_to_blog( $import_coordinates->source_blog_id ); |
158
|
|
|
$source_thumbnail_id = $this->factory()->attachment->create_upload_object( msls_test_data( 'images/image-one.jpg' ) ); |
159
|
|
|
set_post_thumbnail( $import_coordinates->source_post, $source_thumbnail_id ); |
160
|
|
|
$source_image_src = wp_get_attachment_image_src( $source_thumbnail_id ); |
161
|
|
|
$source_image_srcset = wp_get_attachment_image_srcset( $source_thumbnail_id ); |
162
|
|
|
|
163
|
|
|
// the import functionality should work starting fron any blog context |
164
|
|
|
restore_current_blog(); |
165
|
|
|
|
166
|
|
|
$obj = new Linking( $import_coordinates, $logger->reveal(), $relations->reveal() ); |
167
|
|
|
|
168
|
|
|
$obj->import( $dest_post_data ); |
169
|
|
|
|
170
|
|
|
switch_to_blog( $import_coordinates->dest_blog_id ); |
171
|
|
|
$dest_post_thumbnail_id = get_post_thumbnail_id( $import_coordinates->dest_post_id ); |
172
|
|
|
$this->assertEquals( $source_image_src, wp_get_attachment_image_src( $dest_post_thumbnail_id ) ); |
173
|
|
|
$this->assertEquals( $source_image_srcset, wp_get_attachment_image_srcset( $dest_post_thumbnail_id ) ); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|