Completed
Push — add/jp-search-sync ( b7e8bf )
by
unknown
956:15 queued 946:09
created

WP_Test_Jetpack_Sync_Search::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
// phpcs:disable Squiz.Commenting
3
// phpcs:disable Generic.Commenting
4
5
use Automattic\Jetpack\Sync\Modules;
6
7
require_jetpack_file( 'modules/contact-form/grunion-contact-form.php' );
8
9
/**
10
 * Testing Jetpack Search Sync
11
 */
12
class WP_Test_Jetpack_Sync_Search extends WP_Test_Jetpack_Sync_Base {
13
	protected $post_id;
14
	protected static $search_sync;
15
16
	public static function setUpBeforeClass() {
17
		parent::setUpBeforeClass();
18
19
		self::$search_sync = Modules::get_module( 'search' );
20
	}
21
22
	public function setUp() {
23
		parent::setUp();
24
25
		// create a post.
26
		$this->post_id = $this->factory->post->create();
27
		$this->sender->do_sync();
28
	}
29
30
	public function test_module_is_enabled() {
31
		$this->assertTrue( (bool) Modules::get_module( 'search' ) );
32
	}
33
34
	/**
35
	 * Data Providers.
36
	 */
37
38 View Code Duplication
	public function get_allowed_postmeta_keys() {
39
		$search_sync = Modules::get_module( 'search' );
40
		$params      = array();
41
		$keys        = $search_sync->get_all_postmeta_keys();
42
		foreach ( $keys as $k ) {
43
			$params[] = array( $k );
44
		}
45
		return $params;
46
	}
47
48 View Code Duplication
	public function get_allowed_taxonomies() {
49
		$search_sync = Modules::get_module( 'search' );
50
		$params      = array();
51
		$keys        = $search_sync->get_all_taxonomies();
52
		foreach ( $keys as $k ) {
53
			$params[] = array( $k );
54
		}
55
		return $params;
56
	}
57
58
	/**
59
	 * Post meta tests.
60
	 */
61
62
	/**
63
	 * @dataProvider get_allowed_postmeta_keys
64
	 */
65
	public function test_add_post_meta( $key ) {
66
		$v = md5( wp_rand() );
67
		add_post_meta( $this->post_id, $key, $v, true );
68
69
		$this->sender->do_sync();
70
		$this->assertEquals(
71
			array( $v ),
72
			$this->server_replica_storage->get_metadata( 'post', $this->post_id, $key )
73
		);
74
		delete_post_meta( $this->post_id, $key, $v );
75
	}
76
77
	public function test_doesn_t_sync_meta() {
78
		add_post_meta( $this->post_id, 'no_sync_jetpack_search', 'foo' );
79
80
		$this->sender->do_sync();
81
82
		$this->assertEquals(
83
			array(),
84
			$this->server_replica_storage->get_metadata( 'post', $this->post_id, 'no_sync_jetpack_search' )
85
		);
86
		delete_post_meta( $this->post_id, 'no_sync_jetpack_search', 'foo' );
87
	}
88
89
	public function test_meta_is_indexable() {
90
		$this->assertTrue( self::$search_sync->is_indexable( 'postmeta', 'jetpack-search-meta0' ) );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Automattic\Jetpack\Sync\Modules\Module as the method is_indexable() does only exist in the following sub-classes of Automattic\Jetpack\Sync\Modules\Module: Automattic\Jetpack\Sync\Modules\Search. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
91
	}
92
93
	public function test_meta_is_not_indexable() {
94
		$this->assertFalse( self::$search_sync->is_indexable( 'postmeta', 'no_one_wants_to_index_me' ), 'no_one_wants_to_index_me' );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Automattic\Jetpack\Sync\Modules\Module as the method is_indexable() does only exist in the following sub-classes of Automattic\Jetpack\Sync\Modules\Module: Automattic\Jetpack\Sync\Modules\Search. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
95
		$this->assertFalse( self::$search_sync->is_indexable( 'postmeta', 'no_one_wants_to_index_me' ), '_no_one_wants_to_index_me' );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Automattic\Jetpack\Sync\Modules\Module as the method is_indexable() does only exist in the following sub-classes of Automattic\Jetpack\Sync\Modules\Module: Automattic\Jetpack\Sync\Modules\Search. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
96
	}
97
98
	public function test_meta_no_overlap() {
99
		$indexed_keys = self::$search_sync->get_all_postmeta_keys();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Automattic\Jetpack\Sync\Modules\Module as the method get_all_postmeta_keys() does only exist in the following sub-classes of Automattic\Jetpack\Sync\Modules\Module: Automattic\Jetpack\Sync\Modules\Search. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
100
		asort( $indexed_keys );
101
		$unindexed_keys = self::$search_sync->get_all_unindexed_postmeta_keys();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Automattic\Jetpack\Sync\Modules\Module as the method get_all_unindexed_postmeta_keys() does only exist in the following sub-classes of Automattic\Jetpack\Sync\Modules\Module: Automattic\Jetpack\Sync\Modules\Search. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
102
		asort( $unindexed_keys );
103
		$this->assertEmpty( array_intersect( $unindexed_keys, $indexed_keys ) );
104
	}
105
106
	/**
107
	 * Important that we double check the specification format since
108
	 * this will often get added to.
109
	 *
110
	 * @dataProvider get_allowed_postmeta_keys
111
	 */
112
	public function test_check_postmeta_spec( $key ) {
113
		$spec = self::$search_sync->get_postmeta_spec( $key );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Automattic\Jetpack\Sync\Modules\Module as the method get_postmeta_spec() does only exist in the following sub-classes of Automattic\Jetpack\Sync\Modules\Module: Automattic\Jetpack\Sync\Modules\Search. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
114
115
		$this->assertIsArray( $spec );
116
		$this->assertThat(
117
			$spec,
118
			$this->logicalOr(
119
				$this->isEmpty(),
120
				$this->arrayHasKey( 'searchable_in_all_content' ),
121
				$this->arrayHasKey( 'available' ),
122
				$this->arrayHasKey( 'alternatives' )
123
			),
124
			'Post meta specification does not match.'
125
		);
126
127
		if ( isset( $spec['searchable_in_all_content'] ) ) {
128
			$this->assertIsBool( $spec['searchable_in_all_content'] );
129
		}
130
		if ( isset( $spec['available'] ) ) {
131
			$this->assertIsBool( $spec['available'] );
132
		}
133
		if ( isset( $spec['alternatives'] ) ) {
134
			$this->assertIsArray( $spec['alternatives'] );
135
		}
136
	}
137
138
	/**
139
	 * Custom Taxonomy Tests.
140
	 */
141
142
	/**
143
	 * @dataProvider get_allowed_taxonomies
144
	 */
145
	public function test_add_taxonomy( $taxonomy ) {
146
		register_taxonomy(
147
			$taxonomy,
148
			'post',
149
			array(
150
				'label'        => __( 'Taxonomy Test', 'jetpack' ),
151
				'rewrite'      => array( 'slug' => $taxonomy ),
152
				'hierarchical' => true,
153
			)
154
		);
155
156
		$term = md5( wp_rand() );
157
		if ( 'post_format' === $taxonomy ) {
158
			// Special case in Core.
159
			$term = 'Standard';
160
		}
161
		$term_obj = wp_insert_term( $term, $taxonomy );
162
		wp_set_post_terms( $this->post_id, array( $term_obj['term_id'] ), $taxonomy, false );
163
		$this->sender->do_sync();
164
165
		// Check taxonomy and added term.
166
		$this->assertEquals(
167
			$this->get_terms( $taxonomy ),
168
			$this->server_replica_storage->get_terms( $taxonomy ),
169
			'Terms on cache site match do not match client site'
170
		);
171
172
		$this->assertEqualsObject(
173
			get_the_terms( $this->post_id, $taxonomy ),
174
			$this->server_replica_storage->get_the_terms( $this->post_id, $taxonomy ),
175
			'Adeded term does not match local term.'
176
		);
177
178
		// clean up - speeds up tests
179
		wp_remove_object_terms( $this->post_id, array( $term_obj['term_id'] ), $taxonomy );
180
		unregister_taxonomy_for_object_type( $taxonomy, 'post' );
181
	}
182
183
	public function test_taxonomy_is_indexable() {
184
		$this->assertTrue( self::$search_sync->is_indexable( 'taxonomy', 'jetpack-search-tag0' ) );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Automattic\Jetpack\Sync\Modules\Module as the method is_indexable() does only exist in the following sub-classes of Automattic\Jetpack\Sync\Modules\Module: Automattic\Jetpack\Sync\Modules\Search. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
185
	}
186
187
	public function test_taxonomy_is_not_indexable() {
188
		$this->assertFalse( self::$search_sync->is_indexable( 'taxonomy', 'no_one_wants_to_index_me' ) );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Automattic\Jetpack\Sync\Modules\Module as the method is_indexable() does only exist in the following sub-classes of Automattic\Jetpack\Sync\Modules\Module: Automattic\Jetpack\Sync\Modules\Search. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
189
	}
190
191
	/**
192
	 * Helpers
193
	 */
194
195
	protected function get_terms( $taxonomy ) {
196
		$terms = get_terms(
197
			array(
198
				'taxonomy'   => $taxonomy,
199
				'hide_empty' => false,
200
			)
201
		);
202
		// We need an array of stdClass rather than WP_Term.
203
		return array_map(
204
			function ( $object ) {
205
				return (object) (array) $object; },
206
			$terms
207
		);
208
	}
209
210
}
211
212