ContentImporterTest::test_import_content()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 46
rs 9.1781
c 0
b 0
f 0
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() );
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<lloc\Msls\MslsMain>.

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.

Loading history...
42
		$instance->set_logger( $this->logger->reveal() );
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<lloc\Msls\ContentImport\ImportLogger>.

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.

Loading history...
43
		$instance->set_relations( $this->relations->reveal() );
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<lloc\Msls\ContentImport\Relations>.

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.

Loading history...
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 );
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->main->verify_nonce() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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 )
0 ignored issues
show
Documentation introduced by
\Prophecy\Argument::type...sls\MslsOptions::class) is of type object<Prophecy\Argument\Token\TypeToken>, but the function expects a object<lloc\Msls\MslsOptions>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method shouldBeCalled cannot be called on $this->relations->should...st_lang, $dest_post_id) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
109
		                ->shouldBeCalled();
110
		$this->logger->merge( null )->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->logger->merge(null) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
111
		$this->logger->save()->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->logger->save() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
112
		$this->relations->merge( null )->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->relations->merge(null) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
113
		$this->relations->create()->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->relations->create() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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 )
0 ignored issues
show
Documentation introduced by
\Prophecy\Argument::type...sls\MslsOptions::class) is of type object<Prophecy\Argument\Token\TypeToken>, but the function expects a object<lloc\Msls\MslsOptions>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->relations->should...st_lang, $dest_post_id) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
165
		                ->shouldNotBeCalled();
166
		$this->logger->merge( null )->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->logger->merge(null) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
167
		$this->logger->save()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->logger->save() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
168
		$this->relations->merge( null )->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->relations->merge(null) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
169
		$this->relations->create()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->relations->create() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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 )
0 ignored issues
show
Documentation introduced by
\Prophecy\Argument::type...sls\MslsOptions::class) is of type object<Prophecy\Argument\Token\TypeToken>, but the function expects a object<lloc\Msls\MslsOptions>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->relations->should...st_lang, $dest_post_id) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
228
		                ->shouldNotBeCalled();
229
		$this->logger->merge( null )->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->logger->merge(null) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
230
		$this->logger->save()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->logger->save() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
231
		$this->relations->merge( null )->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->relations->merge(null) (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
232
		$this->relations->create()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->relations->create() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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