Skip to content

Commit f95c8cf

Browse files
authored
Page test cases (#18)
* Created base test * Page tests cases
1 parent 2594b36 commit f95c8cf

File tree

11 files changed

+335
-108
lines changed

11 files changed

+335
-108
lines changed

phpcs.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@
1414

1515
<exclude-pattern>*/node_modules/*</exclude-pattern>
1616
<exclude-pattern>*/vendor/*</exclude-pattern>
17+
18+
<!-- excludes -->
19+
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
20+
<exclude-pattern>*/test/suites/*</exclude-pattern>
21+
</rule>
22+
<rule ref="WordPress.Files.FileName.NotHyphenatedLowercase">
23+
<exclude-pattern>*/test/suites/*</exclude-pattern>
24+
</rule>
1725
</ruleset>

test/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ function _manually_load_plugin() {
3030
// Start up the WP testing environment.
3131
require $_tests_dir . '/includes/bootstrap.php';
3232

33+
require 'class-basetestcase.php';
3334
require 'class-permalinksteps.php';
3435
require 'class-permalinkasserter.php';
36+
require 'class-navigationasserter.php';

test/class-basetestcase.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Base Tests case.
4+
*
5+
* @package WordPress_Custom_Fields_Permalink
6+
*/
7+
8+
/**
9+
* Class BaseTestCase
10+
*/
11+
class BaseTestCase extends WP_UnitTestCase {
12+
13+
/**
14+
* The PermalinkSteps.
15+
*
16+
* @var PermalinkSteps
17+
*/
18+
protected $permalink_steps;
19+
20+
/**
21+
* The PermalinkAsserter.
22+
*
23+
* @var PermalinkAsserter
24+
*/
25+
protected $permalink_asserter;
26+
27+
/**
28+
* The NavigationAsserter.
29+
*
30+
* @var NavigationAsserter
31+
*/
32+
protected $navigation_asserter;
33+
34+
/**
35+
* Set up test.
36+
*/
37+
public function setUp() {
38+
parent::setUp();
39+
40+
$this->permalink_steps = new PermalinkSteps( $this );
41+
$this->permalink_asserter = new PermalinkAsserter( $this );
42+
$this->navigation_asserter = new NavigationAsserter( $this );
43+
}
44+
}

test/class-navigationasserter.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Tests util file.
4+
*
5+
* @package WordPress_Custom_Fields_Permalink
6+
*/
7+
8+
/**
9+
* Class NavigationAsserter contains utility methods to assert conditions related to navigation.
10+
*/
11+
class NavigationAsserter {
12+
13+
/**
14+
* Parent unit test case.
15+
*
16+
* @var WP_UnitTestCase
17+
*/
18+
private $unit_test_case;
19+
20+
/**
21+
* NavigationAsserter constructor.
22+
*
23+
* @param WP_UnitTestCase $unit_test_case Parent unit test case.
24+
*/
25+
public function __construct( WP_UnitTestCase $unit_test_case ) {
26+
$this->unit_test_case = $unit_test_case;
27+
}
28+
29+
/**
30+
* Checks if post is displayed
31+
*
32+
* @param WP_Post|int $post The post or id.
33+
*
34+
* @return NavigationAsserter Fluent interface.
35+
*/
36+
public function then_displayed_post( $post ) {
37+
$this->unit_test_case->assertTrue( is_single( $post ) );
38+
return $this;
39+
}
40+
41+
/**
42+
* Checks if post is not displayed
43+
*
44+
* @param WP_Post|int $post The post or id.
45+
*
46+
* @return NavigationAsserter Fluent interface.
47+
*/
48+
public function then_not_displayed_post( $post ) {
49+
$this->unit_test_case->assertFalse( is_single( $post ) );
50+
return $this;
51+
}
52+
53+
/**
54+
* Checks if page is displayed
55+
*
56+
* @param WP_Post|int $post The post or id.
57+
*
58+
* @return NavigationAsserter Fluent interface.
59+
*/
60+
public function then_displayed_page( $post ) {
61+
$this->unit_test_case->assertTrue( is_page( $post ) );
62+
return $this;
63+
}
64+
65+
/**
66+
* Checks if page is not displayed
67+
*
68+
* @param WP_Post|int $post The post or id.
69+
*
70+
* @return NavigationAsserter Fluent interface.
71+
*/
72+
public function then_not_displayed_page( $post ) {
73+
$this->unit_test_case->assertFalse( is_page( $post ) );
74+
return $this;
75+
}
76+
77+
/**
78+
* Checks if 404 page is reached
79+
*
80+
* @return NavigationAsserter Fluent interface.
81+
*/
82+
public function then_is_404() {
83+
$this->unit_test_case->assertTrue( is_404() );
84+
return $this;
85+
}
86+
87+
/**
88+
* Fluent interface.
89+
*
90+
* @return NavigationAsserter Fluent interface.
91+
*/
92+
public function and_also() {
93+
return $this;
94+
}
95+
96+
}

test/class-permalinkasserter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,23 @@ public function __construct( WP_UnitTestCase $unit_test_case ) {
3131
*
3232
* @param WP_Post $post The post.
3333
* @param string $permalink Expected permalink.
34+
*
35+
* @return PermalinkAsserter Fluent interface.
3436
*/
3537
public function has_permalink( $post, $permalink ) {
3638
$actual = wp_make_link_relative( get_the_permalink( $post ) );
3739

3840
$this->unit_test_case->assertEquals( $permalink, $actual );
41+
42+
return $this;
43+
}
44+
45+
/**
46+
* Fluent interface.
47+
*
48+
* @return PermalinkAsserter Fluent interface.
49+
*/
50+
public function and_also() {
51+
return $this;
3952
}
4053
}

test/suites/BasicPostNamePermalinkStructure/basicpostnamepermalinkstructure.php renamed to test/suites/BasicPostNamePermalinkStructure/BasicPostNamePermalinkStructure.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,7 @@
88
/**
99
* Class BasicPostNamePermalinkStructure
1010
*/
11-
class BasicPostNamePermalinkStructure extends WP_UnitTestCase {
12-
13-
/**
14-
* The PermalinkSteps.
15-
*
16-
* @var PermalinkSteps
17-
*/
18-
private $permalink_steps;
19-
20-
/**
21-
* The PermalinkAsserter.
22-
*
23-
* @var PermalinkAsserter
24-
*/
25-
private $permalink_asserter;
26-
27-
/**
28-
* Set up test.
29-
*/
30-
public function setUp() {
31-
parent::setUp();
32-
33-
$this->permalink_steps = new PermalinkSteps( $this );
34-
$this->permalink_asserter = new PermalinkAsserter( $this );
35-
}
11+
class BasicPostNamePermalinkStructure extends BaseTestCase {
3612

3713
/**
3814
* Test case.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Tests case.
4+
*
5+
* @package WordPress_Custom_Fields_Permalink
6+
*/
7+
8+
/**
9+
* Class PageWithMetaKey
10+
*
11+
* Those test cases proves the default WordPress functionality which
12+
* displays pages without using permalink structure.
13+
*/
14+
class PageWithMetaKey extends BaseTestCase {
15+
16+
/**
17+
* Test case.
18+
*/
19+
function test_generates_permalink_to_page_not_using_meta_key() {
20+
// given.
21+
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
22+
23+
$page_params = [
24+
'post_type' => 'page',
25+
'post_title' => 'Some page title',
26+
'meta_input' => [
27+
'some_meta_key' => 'Some meta value',
28+
'some_other_meta_key' => 'Some other meta value',
29+
],
30+
];
31+
$created_page_id = $this->factory()->post->create( $page_params );
32+
33+
// when & then.
34+
$this->permalink_asserter->has_permalink( $created_page_id, '/some-page-title/' );
35+
}
36+
37+
/**
38+
* Test case.
39+
*/
40+
function IGNORE_test_go_to_page_not_using_meta_key_permalink_structure() {
41+
// given.
42+
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
43+
44+
$post_params = [
45+
'post_type' => 'page',
46+
'post_title' => 'Some page title',
47+
'meta_input' => [
48+
'some_meta_key' => 'Some meta value',
49+
'some_other_meta_key' => 'Some other meta value',
50+
],
51+
];
52+
$created_page_id = $this->factory()->post->create( $post_params );
53+
54+
// when.
55+
$this->go_to( '/some-page-title/' );
56+
57+
// then.
58+
$this->navigation_asserter->then_displayed_page( $created_page_id );
59+
}
60+
}

test/suites/MetaKeyPermalinkStructure/postwithduplicatedmetakey.php renamed to test/suites/MetaKeyPermalinkStructure/PostWithDuplicatedMetaKey.php

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,7 @@
88
/**
99
* Class PostWithDuplicatedMetaKey
1010
*/
11-
class PostWithDuplicatedMetaKey extends WP_UnitTestCase {
12-
13-
/**
14-
* The PermalinkSteps.
15-
*
16-
* @var PermalinkSteps
17-
*/
18-
private $permalink_steps;
19-
20-
/**
21-
* The PermalinkAsserter.
22-
*
23-
* @var PermalinkAsserter
24-
*/
25-
private $permalink_asserter;
26-
27-
/**
28-
* Set up test.
29-
*/
30-
public function setUp() {
31-
parent::setUp();
32-
33-
$this->permalink_steps = new PermalinkSteps( $this );
34-
$this->permalink_asserter = new PermalinkAsserter( $this );
35-
}
11+
class PostWithDuplicatedMetaKey extends BaseTestCase {
3612

3713
/**
3814
* Test case.
@@ -80,8 +56,7 @@ function test_go_to_post_when_duplicated_meta_key_and_use_first_one() {
8056
$this->go_to( '/some-meta-value/some-post-title/' );
8157

8258
// then.
83-
$this->assertFalse( is_404() );
84-
$this->assertEquals( $created_post_id, get_the_ID() );
59+
$this->navigation_asserter->then_displayed_post( $created_post_id );
8560
}
8661

8762
/**
@@ -105,7 +80,6 @@ function test_go_to_post_when_duplicated_meta_key_and_use_duplicate_one() {
10580
$this->go_to( '/some-duplicated-meta-value/some-post-title/' );
10681

10782
// then.
108-
$this->assertFalse( is_404() );
109-
$this->assertEquals( $created_post_id, get_the_ID() );
83+
$this->navigation_asserter->then_displayed_post( $created_post_id );
11084
}
11185
}

0 commit comments

Comments
 (0)