|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace lloc\Msls\ContentImport; |
|
4
|
|
|
|
|
5
|
|
|
use lloc\Msls\ContentImport\Importers\Importer; |
|
6
|
|
|
use lloc\Msls\MslsMain; |
|
7
|
|
|
use lloc\Msls\MslsOptions; |
|
8
|
|
|
use Prophecy\Argument; |
|
9
|
|
|
|
|
10
|
|
|
class ContentImporterTest extends \Msls_UnitTestCase { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var \lloc\Msls\MslsMain |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $main; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var \lloc\Msls\ContentImport\ImportLogger |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $logger; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \lloc\Msls\ContentImport\Relations |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $relations; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @test |
|
29
|
|
|
* it should be instantiatable |
|
30
|
|
|
*/ |
|
31
|
|
|
public function it_should_be_instantiatable() { |
|
32
|
|
|
$sut = $this->make_instance(); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertInstanceOf( ContentImporter::class, $sut ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return ContentImporter |
|
39
|
|
|
*/ |
|
40
|
|
|
private function make_instance() { |
|
41
|
|
|
$instance = new ContentImporter( $this->main->reveal() ); |
|
|
|
|
|
|
42
|
|
|
$instance->set_logger( $this->logger->reveal() ); |
|
|
|
|
|
|
43
|
|
|
$instance->set_relations( $this->relations->reveal() ); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
return $instance; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function filter_empty_inputs() { |
|
49
|
|
|
return [ |
|
50
|
|
|
// $input, $nonce_verified, $POST_vars, $expected |
|
51
|
|
|
[ 'foo', false, [ 'msls_import' => 'some-value' ], 'foo' ], |
|
52
|
|
|
[ 'foo', true, [], 'foo' ], |
|
53
|
|
|
[ 'foo', true, [ 'msls_import' => 'some-value' ], false ], |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @dataProvider filter_empty_inputs |
|
59
|
|
|
*/ |
|
60
|
|
|
public function test_filter_empty( $input, $nonce_verified, $POST_vars, $expected ) { |
|
61
|
|
|
$this->main->verify_nonce()->willReturn( $nonce_verified ); |
|
|
|
|
|
|
62
|
|
|
unset( $_POST['msls_import'] ); |
|
63
|
|
|
$_POST = array_merge( $_POST, $POST_vars ); |
|
64
|
|
|
|
|
65
|
|
|
$obj = $this->make_instance(); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals( $expected, $obj->filter_empty( $input ) ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function import_content_inputs() { |
|
71
|
|
|
return [ |
|
72
|
|
|
[ 'en_US', 'de_DE' ], |
|
73
|
|
|
[ 'de_DE', 'en_US' ], |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @dataProvider import_content_inputs |
|
79
|
|
|
*/ |
|
80
|
|
|
public function test_import_content( $source_lang, $dest_lang ) { |
|
81
|
|
|
$source_blog_id = $this->factory->blog->create(); |
|
82
|
|
|
$dest_blog_id = $this->factory->blog->create(); |
|
83
|
|
|
|
|
84
|
|
|
switch_to_blog( $source_blog_id ); |
|
85
|
|
|
$source_post = $this->factory->post->create_and_get(); |
|
86
|
|
|
$source_post_id = $source_post->ID; |
|
87
|
|
|
update_option( 'WPLANG', $source_lang ); |
|
88
|
|
|
|
|
89
|
|
|
switch_to_blog( $dest_blog_id ); |
|
90
|
|
|
$dest_post_id = $this->factory->post->create(); |
|
91
|
|
|
update_option( 'WPLANG', $dest_lang ); |
|
92
|
|
|
|
|
93
|
|
|
$import_coordinates = new ImportCoordinates(); |
|
94
|
|
|
$import_coordinates->source_blog_id = $source_blog_id; |
|
95
|
|
|
$import_coordinates->source_post_id = $source_post_id; |
|
96
|
|
|
$import_coordinates->dest_blog_id = $dest_blog_id; |
|
97
|
|
|
$import_coordinates->dest_post_id = $dest_post_id; |
|
98
|
|
|
$import_coordinates->source_post = $source_post; |
|
99
|
|
|
$import_coordinates->source_lang = $source_lang; |
|
100
|
|
|
$import_coordinates->dest_lang = $dest_lang; |
|
101
|
|
|
$data = [ 'foo' => 'bar' ]; |
|
102
|
|
|
$importer = $this->prophesize( Importer::class ); |
|
103
|
|
|
$importer->import( $data ) |
|
104
|
|
|
->willReturn( $data ); |
|
105
|
|
|
$importer->get_logger()->willReturn( null ); |
|
106
|
|
|
$importer->get_relations()->willReturn( null ); |
|
107
|
|
|
|
|
108
|
|
|
$this->relations->should_create( Argument::type( MslsOptions::class ), $dest_lang, $dest_post_id ) |
|
|
|
|
|
|
109
|
|
|
->shouldBeCalled(); |
|
110
|
|
|
$this->logger->merge( null )->shouldBeCalled(); |
|
|
|
|
|
|
111
|
|
|
$this->logger->save()->shouldBeCalled(); |
|
|
|
|
|
|
112
|
|
|
$this->relations->merge( null )->shouldBeCalled(); |
|
|
|
|
|
|
113
|
|
|
$this->relations->create()->shouldBeCalled(); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
add_filter( 'msls_content_import_importers', function () use ( $importer ) { |
|
116
|
|
|
return [ |
|
117
|
|
|
'test-importer' => $importer->reveal(), |
|
118
|
|
|
]; |
|
119
|
|
|
} ); |
|
120
|
|
|
|
|
121
|
|
|
$obj = $this->make_instance(); |
|
122
|
|
|
$updated_data = $obj->import_content( $import_coordinates, $data ); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertEquals( $data, $updated_data ); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function test_import_content_with_wrong_coordinates() { |
|
128
|
|
|
$data = [ 'foo' => 'bar' ]; |
|
129
|
|
|
$obj = $this->make_instance(); |
|
130
|
|
|
$import_coordinates = $this->prophesize( ImportCoordinates::class ); |
|
131
|
|
|
$import_coordinates->validate()->willReturn( false ); |
|
132
|
|
|
|
|
133
|
|
|
$updated_data = $obj->import_content( $import_coordinates->reveal(), $data ); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertEquals( $data, $updated_data ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function test_content_import_without_importers() { |
|
139
|
|
|
$source_lang = 'de_DE'; |
|
140
|
|
|
$dest_lang = 'en_US'; |
|
141
|
|
|
|
|
142
|
|
|
$source_blog_id = $this->factory->blog->create(); |
|
143
|
|
|
$dest_blog_id = $this->factory->blog->create(); |
|
144
|
|
|
|
|
145
|
|
|
switch_to_blog( $source_blog_id ); |
|
146
|
|
|
$source_post = $this->factory->post->create_and_get(); |
|
147
|
|
|
$source_post_id = $source_post->ID; |
|
148
|
|
|
update_option( 'WPLANG', $source_lang ); |
|
149
|
|
|
|
|
150
|
|
|
switch_to_blog( $dest_blog_id ); |
|
151
|
|
|
$dest_post_id = $this->factory->post->create(); |
|
152
|
|
|
update_option( 'WPLANG', $dest_lang ); |
|
153
|
|
|
|
|
154
|
|
|
$import_coordinates = new ImportCoordinates(); |
|
155
|
|
|
$import_coordinates->source_blog_id = $source_blog_id; |
|
156
|
|
|
$import_coordinates->source_post_id = $source_post_id; |
|
157
|
|
|
$import_coordinates->dest_blog_id = $dest_blog_id; |
|
158
|
|
|
$import_coordinates->dest_post_id = $dest_post_id; |
|
159
|
|
|
$import_coordinates->source_post = $source_post; |
|
160
|
|
|
$import_coordinates->source_lang = $source_lang; |
|
161
|
|
|
$import_coordinates->dest_lang = $dest_lang; |
|
162
|
|
|
$data = [ 'foo' => 'bar' ]; |
|
163
|
|
|
$importer = $this->prophesize( Importer::class ); |
|
164
|
|
|
$this->relations->should_create( Argument::type( MslsOptions::class ), $dest_lang, $dest_post_id ) |
|
|
|
|
|
|
165
|
|
|
->shouldNotBeCalled(); |
|
166
|
|
|
$this->logger->merge( null )->shouldNotBeCalled(); |
|
|
|
|
|
|
167
|
|
|
$this->logger->save()->shouldNotBeCalled(); |
|
|
|
|
|
|
168
|
|
|
$this->relations->merge( null )->shouldNotBeCalled(); |
|
|
|
|
|
|
169
|
|
|
$this->relations->create()->shouldNotBeCalled(); |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
add_filter( 'msls_content_import_importers', function () use ( $importer ) { |
|
172
|
|
|
return []; |
|
173
|
|
|
} ); |
|
174
|
|
|
|
|
175
|
|
|
$obj = $this->make_instance(); |
|
176
|
|
|
$updated_data = $obj->import_content( $import_coordinates, $data ); |
|
177
|
|
|
|
|
178
|
|
|
$this->assertEquals( $data, $updated_data ); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function parse_sources_inputs() { |
|
182
|
|
|
return [ |
|
183
|
|
|
[ null, false ], |
|
184
|
|
|
[ 'foo', false ], |
|
185
|
|
|
[ 'foo|bar', false ], |
|
186
|
|
|
[ '23|bar', false ], |
|
187
|
|
|
[ 'foo|89', false ], |
|
188
|
|
|
[ '23|89', [ 23, 89 ] ], |
|
189
|
|
|
]; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @dataProvider parse_sources_inputs |
|
194
|
|
|
*/ |
|
195
|
|
|
public function test_parse_sources( $sources, $expected ) { |
|
196
|
|
|
$obj = $this->make_instance(); |
|
197
|
|
|
|
|
198
|
|
|
unset( $_POST['msls_import'] ); |
|
199
|
|
|
if ( null !== $sources ) { |
|
200
|
|
|
$_POST['msls_import'] = $sources; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$this->assertEquals( $expected, $obj->parse_sources() ); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function test_on_wp_insert_post_with_source_and_current_same() { |
|
207
|
|
|
$source_lang = 'de_DE'; |
|
208
|
|
|
$dest_lang = 'en_US'; |
|
209
|
|
|
|
|
210
|
|
|
$source_blog_id = $this->factory->blog->create(); |
|
211
|
|
|
$dest_blog_id = $this->factory->blog->create(); |
|
212
|
|
|
|
|
213
|
|
|
switch_to_blog( $source_blog_id ); |
|
214
|
|
|
$source_post = $this->factory->post->create_and_get(); |
|
215
|
|
|
$source_post_id = $source_post->ID; |
|
216
|
|
|
update_option( 'WPLANG', $source_lang ); |
|
217
|
|
|
|
|
218
|
|
|
switch_to_blog( $dest_blog_id ); |
|
219
|
|
|
$dest_post_id = $this->factory->post->create(); |
|
220
|
|
|
update_option( 'WPLANG', $dest_lang ); |
|
221
|
|
|
|
|
222
|
|
|
$data = [ 'foo' => 'bar' ]; |
|
223
|
|
|
$importer = $this->prophesize( Importer::class ); |
|
224
|
|
|
|
|
225
|
|
|
$_POST['msls_import'] = "{$source_blog_id}|{$source_post_id}"; |
|
226
|
|
|
|
|
227
|
|
|
$this->relations->should_create( Argument::type( MslsOptions::class ), $dest_lang, $dest_post_id ) |
|
|
|
|
|
|
228
|
|
|
->shouldNotBeCalled(); |
|
229
|
|
|
$this->logger->merge( null )->shouldNotBeCalled(); |
|
|
|
|
|
|
230
|
|
|
$this->logger->save()->shouldNotBeCalled(); |
|
|
|
|
|
|
231
|
|
|
$this->relations->merge( null )->shouldNotBeCalled(); |
|
|
|
|
|
|
232
|
|
|
$this->relations->create()->shouldNotBeCalled(); |
|
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
add_filter( 'msls_content_import_importers', function () use ( $importer ) { |
|
235
|
|
|
return [ |
|
236
|
|
|
'test-importer' => $importer->reveal(), |
|
237
|
|
|
]; |
|
238
|
|
|
} ); |
|
239
|
|
|
|
|
240
|
|
|
$obj = $this->make_instance(); |
|
241
|
|
|
|
|
242
|
|
|
switch_to_blog( $source_blog_id ); |
|
243
|
|
|
$updated_data = $obj->handle_import( $data ); |
|
244
|
|
|
|
|
245
|
|
|
$this->assertEquals( $data, $updated_data ); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
function setUp() { |
|
249
|
|
|
parent::setUp(); |
|
250
|
|
|
$this->main = $this->prophesize( MslsMain::class ); |
|
251
|
|
|
$this->logger = $this->prophesize( ImportLogger::class ); |
|
252
|
|
|
$this->relations = $this->prophesize( Relations::class ); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.