Completed
Push — master ( 6c0735...dd8543 )
by Dennis
06:47
created

ImportCoordinatesTest::testValidate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
1
<?php
2
3
namespace lloc\Msls\ContentImport;
4
5
6
class ImportCoordinatesTest extends \Msls_UnitTestCase {
7
8
	function setUp() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
setUp uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setUp uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setUp uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
9
		parent::setUp();
10
		unset(
11
			$_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ],
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
12
			$_POST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ],
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
13
			$_GET[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ]
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
14
		);
15
	}
16
17
	public function testValidate() {
0 ignored issues
show
Coding Style introduced by
The function name testValidate is in camel caps, but expected test_validate instead as per the coding standard.
Loading history...
18
		$dest_lang   = 'en_US';
19
		$source_lang = 'de_DE';
20
21
		$obj = new ImportCoordinates();
22
23
		$this->assertFalse( $obj->validate() );
24
25
		$source_blog_id      = $this->factory->blog->create();
26
		$obj->source_blog_id = $source_blog_id;
27
28
		$this->assertFalse( $obj->validate() );
29
30
		switch_to_blog( $source_blog_id );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
31
		$source_post    = $this->factory->post->create_and_get();
32
		$source_post_id = $source_post->ID;
33
34
		$obj->source_post_id = $source_post_id;
35
		$obj->source_post    = $source_post;
36
37
		$this->assertFalse( $obj->validate() );
38
39
		$dest_blog_id = $this->factory->blog->create();
40
41
		$obj->dest_blog_id = $dest_blog_id;
42
43
		$this->assertFalse( $obj->validate() );
44
45
		update_option( 'WPLANG', $source_lang );
46
47
		$obj->source_lang = $source_lang;
48
49
		$this->assertFalse( $obj->validate() );
50
51
		switch_to_blog( $dest_blog_id );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
52
		$dest_post_id = $this->factory->post->create();
53
54
		$obj->dest_post_id = $dest_post_id;
55
56
		$this->assertFalse( $obj->validate() );
57
58
		update_option( 'WPLANG', $dest_lang );
59
60
		$obj->dest_lang = $dest_lang;
61
62
		$this->assertTrue( $obj->validate() );
63
	}
64
65
	/**
66
	 * Test set_importer_for
67
	 */
68
	public function test_set_importer_for() {
69
		$obj = new ImportCoordinates();
70
71
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
72
73
		$obj->set_importer_for( 'foo', 'bar' );
74
75
		$this->assertEquals( 'bar', $obj->get_importer_for( 'foo' ) );
76
77
		$this->assertEmpty( $obj->get_importer_for( 'baz' ) );
78
	}
79
80
	/**
81
	 * Test parse_importers from REQUEST
82
	 */
83
	public function test_parse_importers_from_REQUEST() {
0 ignored issues
show
Coding Style introduced by
test_parse_importers_from_REQUEST uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
The function name test_parse_importers_from_REQUEST is in camel caps, but expected test_parse_importers_from_r_e_q_u_e_s_t instead as per the coding standard.
Loading history...
84
		$obj = new ImportCoordinates();
85
86
		unset( $_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
87
88
		$obj->parse_importers_from_request();
89
90
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
91
92
		$_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [];
93
94
		$obj->parse_importers_from_request();
95
96
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
97
98
		$_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [ 'foo' => 'bar' ];
99
100
		$obj->parse_importers_from_request();
101
102
		$this->assertEquals( 'bar', $obj->get_importer_for( 'foo' ) );
103
	}
104
105
	/**
106
	 * Test parse_importers from POST
107
	 */
108
	public function test_parse_importers_from_POST() {
0 ignored issues
show
Coding Style introduced by
test_parse_importers_from_POST uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
The function name test_parse_importers_from_POST is in camel caps, but expected test_parse_importers_from_p_o_s_t instead as per the coding standard.
Loading history...
109
		$obj = new ImportCoordinates();
110
111
		unset( $_POST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
112
113
		$obj->parse_importers_from_request();
114
115
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
116
117
		$_POST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [];
118
119
		$obj->parse_importers_from_request();
120
121
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
122
123
		$_POST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [ 'foo' => 'bar' ];
124
125
		$obj->parse_importers_from_request();
126
127
		$this->assertEquals( 'bar', $obj->get_importer_for( 'foo' ) );
128
	}
129
130
	/**
131
	 * Test parse_importers from GET
132
	 */
133
	public function test_parse_importers_from_GET() {
0 ignored issues
show
Coding Style introduced by
test_parse_importers_from_GET uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
The function name test_parse_importers_from_GET is in camel caps, but expected test_parse_importers_from_g_e_t instead as per the coding standard.
Loading history...
134
		$obj = new ImportCoordinates();
135
136
		unset( $_GET[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
137
138
		$obj->parse_importers_from_request();
139
140
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
141
142
		$_GET[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [];
143
144
		$obj->parse_importers_from_request();
145
146
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
147
148
		$_GET[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [ 'foo' => 'bar' ];
149
150
		$obj->parse_importers_from_request();
151
152
		$this->assertEquals( 'bar', $obj->get_importer_for( 'foo' ) );
153
	}
154
155
	/**
156
	 * Test parse_importers from REQUEST and POST
157
	 */
158
	public function test_parse_importers_from_REQUEST_and_POST() {
0 ignored issues
show
Coding Style introduced by
test_parse_importers_from_REQUEST_and_POST uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
test_parse_importers_from_REQUEST_and_POST uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
The function name test_parse_importers_from_REQUEST_and_POST is in camel caps, but expected test_parse_importers_fro...e_q_u_e_s_t_and_p_o_s_t instead as per the coding standard.
Loading history...
159
		$obj = new ImportCoordinates();
160
161
		unset( $_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
162
		unset( $_POST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
163
164
		$obj->parse_importers_from_request();
165
166
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
167
168
		$_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [];
169
		$_POST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ]    = [ 'some' => 'baz' ];
170
171
		$obj->parse_importers_from_request();
172
173
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
174
		$this->assertEmpty( $obj->get_importer_for( 'some' ) );
175
176
		$_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [ 'foo' => 'bar' ];
177
		$_POST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ]    = [ 'some' => 'baz' ];
178
179
		$obj->parse_importers_from_request();
180
181
		$this->assertEquals( 'bar', $obj->get_importer_for( 'foo' ) );
182
		$this->assertEmpty( $obj->get_importer_for( 'some' ) );
183
	}
184
185
	/**
186
	 * Test parse_importers from REQUEST and GET
187
	 */
188
	public function test_parse_importers_from_REQUEST_and_GET() {
0 ignored issues
show
Coding Style introduced by
test_parse_importers_from_REQUEST_and_GET uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
test_parse_importers_from_REQUEST_and_GET uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
The function name test_parse_importers_from_REQUEST_and_GET is in camel caps, but expected test_parse_importers_from_r_e_q_u_e_s_t_and_g_e_t instead as per the coding standard.
Loading history...
189
		$obj = new ImportCoordinates();
190
191
		unset( $_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
192
		unset( $_GET[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
193
194
		$obj->parse_importers_from_request();
195
196
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
197
198
		$_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [];
199
		$_GET[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ]     = [ 'some' => 'baz' ];
200
201
		$obj->parse_importers_from_request();
202
203
		$this->assertEmpty( $obj->get_importer_for( 'foo' ) );
204
		$this->assertEmpty( $obj->get_importer_for( 'some' ) );
205
206
		$_REQUEST[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ] = [ 'foo' => 'bar' ];
207
		$_GET[ ImportCoordinates::IMPORTERS_GLOBAL_KEY ]     = [ 'some' => 'baz' ];
208
209
		$obj->parse_importers_from_request();
210
211
		$this->assertEquals( 'bar', $obj->get_importer_for( 'foo' ) );
212
		$this->assertEmpty( $obj->get_importer_for( 'some' ) );
213
	}
214
}
215