Completed
Push — add/jp-search-sync ( 59fc08...405a37 )
by
unknown
552:47 queued 542:28
created

Test_Jetpack_Sync_Search::test_module_is_enabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Integration Tests for Search syncing.
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
// phpcs:disable Squiz.Commenting
9
// phpcs:disable Generic.Commenting
10
11
use Automattic\Jetpack\Sync\Modules;
12
13
class Test_Jetpack_Sync_Search extends WP_Test_Jetpack_Sync_Base {
14
	protected $post_id;
15
	protected static $search_sync;
16
17
	public static function setUpBeforeClass() {
18
		parent::setUpBeforeClass();
19
20
		self::$search_sync = Modules::get_module( 'search' );
21
	}
22
23
	public function setUp() {
24
		parent::setUp();
25
26
		// create a post.
27
		$this->post_id = $this->factory->post->create();
28
		$this->sender->do_sync();
29
	}
30
31
	public function test_module_is_enabled() {
32
		$this->assertTrue( (bool) Modules::get_module( 'search' ) );
33
	}
34
35
	/**
36
	 * Data Providers.
37
	 */
38
39 View Code Duplication
	public function get_allowed_postmeta_keys() {
40
		$search_sync = Modules::get_module( 'search' );
41
		$params      = array();
42
		$keys        = $search_sync->get_all_postmeta_keys();
43
		foreach ( $keys as $k ) {
44
			$params[] = array( $k );
45
		}
46
		return $params;
47
	}
48
49 View Code Duplication
	public function get_allowed_taxonomies() {
50
		$search_sync = Modules::get_module( 'search' );
51
		$params      = array();
52
		$keys        = $search_sync->get_all_taxonomies();
53
		foreach ( $keys as $k ) {
54
			$params[] = array( $k );
55
		}
56
		return $params;
57
	}
58
59
	/**
60
	 * Post meta tests.
61
	 */
62
63
	/**
64
	 * @dataProvider get_allowed_postmeta_keys
65
	 */
66
	public function test_add_post_meta( $key ) {
67
		$v = md5( wp_rand() );
68
		add_post_meta( $this->post_id, $key, $v, true );
69
70
		$this->sender->do_sync();
71
		$this->assertEquals(
72
			array( $v ),
73
			$this->server_replica_storage->get_metadata( 'post', $this->post_id, $key )
74
		);
75
		delete_post_meta( $this->post_id, $key, $v );
76
	}
77
78
	public function test_doesn_t_sync_meta() {
79
		add_post_meta( $this->post_id, 'no_sync_jetpack_search', 'foo' );
80
81
		$this->sender->do_sync();
82
83
		$this->assertEquals(
84
			array(),
85
			$this->server_replica_storage->get_metadata( 'post', $this->post_id, 'no_sync_jetpack_search' )
86
		);
87
		delete_post_meta( $this->post_id, 'no_sync_jetpack_search', 'foo' );
88
	}
89
90
	public function test_meta_is_indexable() {
91
		$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...
92
	}
93
94
	public function test_meta_is_not_indexable() {
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
		$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...
97
	}
98
99
	public function test_meta_no_overlap() {
100
		$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...
101
		asort( $indexed_keys );
102
		$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...
103
		asort( $unindexed_keys );
104
		$this->assertEmpty( array_intersect( $unindexed_keys, $indexed_keys ) );
105
	}
106
107
	/**
108
	 * Important that we double check the specification format since
109
	 * this will often get added to.
110
	 *
111
	 * @dataProvider get_allowed_postmeta_keys
112
	 */
113
	public function test_check_postmeta_spec( $key ) {
114
		$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...
115
116
		$this->assertIsArray( $spec );
117
		$this->assertThat(
118
			$spec,
119
			$this->logicalOr(
120
				$this->isEmpty(),
121
				$this->arrayHasKey( 'searchable_in_all_content' ),
122
				$this->arrayHasKey( 'available' ),
123
				$this->arrayHasKey( 'alternatives' )
124
			),
125
			'Post meta specification does not match.'
126
		);
127
128
		if ( isset( $spec['searchable_in_all_content'] ) ) {
129
			$this->assertIsBool( $spec['searchable_in_all_content'] );
130
		}
131
		if ( isset( $spec['available'] ) ) {
132
			$this->assertIsBool( $spec['available'] );
133
		}
134
		if ( isset( $spec['alternatives'] ) ) {
135
			$this->assertIsArray( $spec['alternatives'] );
136
		}
137
	}
138
139
	/**
140
	 * Custom Taxonomy Tests.
141
	 */
142
143
	/**
144
	 * @dataProvider get_allowed_taxonomies
145
	 */
146
	public function test_add_taxonomy( $taxonomy ) {
147
		register_taxonomy(
148
			$taxonomy,
149
			'post',
150
			array(
151
				'label'        => __( 'Taxonomy Test', 'jetpack' ),
152
				'rewrite'      => array( 'slug' => $taxonomy ),
153
				'hierarchical' => true,
154
			)
155
		);
156
157
		$term = md5( wp_rand() );
158
		if ( 'post_format' === $taxonomy ) {
159
			// Special case in Core.
160
			$term = 'Standard';
161
		}
162
		$term_obj = wp_insert_term( $term, $taxonomy );
163
		wp_set_post_terms( $this->post_id, array( $term_obj['term_id'] ), $taxonomy, false );
164
		$this->sender->do_sync();
165
166
		// Check taxonomy and added term.
167
		$this->assertEquals(
168
			$this->get_terms( $taxonomy ),
169
			$this->server_replica_storage->get_terms( $taxonomy ),
170
			'Terms on cache site match do not match client site'
171
		);
172
173
		$this->assertEqualsObject(
174
			get_the_terms( $this->post_id, $taxonomy ),
175
			$this->server_replica_storage->get_the_terms( $this->post_id, $taxonomy ),
176
			'Adeded term does not match local term.'
177
		);
178
179
		// clean up - speeds up tests
180
		wp_remove_object_terms( $this->post_id, array( $term_obj['term_id'] ), $taxonomy );
181
		unregister_taxonomy_for_object_type( $taxonomy, 'post' );
182
	}
183
184
	public function test_taxonomy_is_indexable() {
185
		$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...
186
	}
187
188
	public function test_taxonomy_is_not_indexable() {
189
		$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...
190
	}
191
192
	/**
193
	 * Helpers
194
	 */
195
196
	protected function get_terms( $taxonomy ) {
197
		$terms = get_terms(
198
			array(
199
				'taxonomy'   => $taxonomy,
200
				'hide_empty' => false,
201
			)
202
		);
203
		// We need an array of stdClass rather than WP_Term.
204
		return array_map(
205
			function ( $object ) {
206
				return (object) (array) $object; },
207
			$terms
208
		);
209
	}
210
211
}
212
213