3
3
namespace Municipio \ExternalContent \Config ;
4
4
5
5
use Municipio \Config \Features \SchemaData \SchemaDataConfigInterface ;
6
+ use Municipio \ExternalContent \Filter \FilterDefinition \Contracts \Enums \Operator ;
6
7
use PHPUnit \Framework \TestCase ;
7
8
use WpService \Implementations \FakeWpService ;
8
9
@@ -40,8 +41,7 @@ public function testAcfConfig()
40
41
*/
41
42
public function testAcfConfigContainsRepeaterField ()
42
43
{
43
- $ jsonFileContents = file_get_contents (__DIR__ . '/../../AcfFields/json/external-content-settings.json ' );
44
- $ json = json_decode ($ jsonFileContents , true );
44
+ $ json = $ this ->getAcfFields ();
45
45
46
46
$ fields = $ json [0 ]['fields ' ];
47
47
@@ -54,8 +54,7 @@ public function testAcfConfigContainsRepeaterField()
54
54
*/
55
55
public function testAcfConfigContainsExpectedSubFields ()
56
56
{
57
- $ jsonFileContents = file_get_contents (__DIR__ . '/../../AcfFields/json/external-content-settings.json ' );
58
- $ json = json_decode ($ jsonFileContents , true );
57
+ $ json = $ this ->getAcfFields ();
59
58
60
59
$ fields = $ json [0 ]['fields ' ];
61
60
@@ -71,15 +70,15 @@ public function testAcfConfigContainsExpectedSubFields()
71
70
$ this ->assertContains ('source_typesense_collection ' , $ subFieldNames );
72
71
$ this ->assertContains ('automatic_import_schedule ' , $ subFieldNames );
73
72
$ this ->assertContains ('taxonomies ' , $ subFieldNames );
73
+ $ this ->assertContains ('rules ' , $ subFieldNames );
74
74
}
75
75
76
76
/**
77
77
* @testdox ACF json taxonomies field contains expected sub fields
78
78
*/
79
79
public function testAcfConfigContainsExpectedTaxonomiesSubFields ()
80
80
{
81
- $ jsonFileContents = file_get_contents (__DIR__ . '/../../AcfFields/json/external-content-settings.json ' );
82
- $ json = json_decode ($ jsonFileContents , true );
81
+ $ json = $ this ->getAcfFields ();
83
82
$ fields = $ json [0 ]['fields ' ];
84
83
$ subFields = $ fields [0 ]['sub_fields ' ];
85
84
$ taxonomiesField = array_values (array_filter ($ subFields , fn ($ subField ) => $ subField ['name ' ] === 'taxonomies ' ));
@@ -91,6 +90,22 @@ public function testAcfConfigContainsExpectedTaxonomiesSubFields()
91
90
$ this ->assertContains ('hierarchical ' , $ taxonomiesSubFieldNames );
92
91
}
93
92
93
+ /**
94
+ * @testdox ACF json taxonomies field contains expected filter sub fields
95
+ */
96
+ public function testAcfConfigContainsExpectedTaxonomiesFilterSubFields ()
97
+ {
98
+ $ json = $ this ->getAcfFields ();
99
+ $ fields = $ json [0 ]['fields ' ];
100
+ $ subFields = $ fields [0 ]['sub_fields ' ];
101
+ $ rulesField = array_values (array_filter ($ subFields , fn ($ subField ) => $ subField ['name ' ] === 'rules ' ));
102
+ $ rulesSubFieldNames = array_map (fn ($ subField ) => $ subField ['name ' ], $ rulesField [0 ]['sub_fields ' ]);
103
+
104
+ $ this ->assertContains ('property_path ' , $ rulesSubFieldNames );
105
+ $ this ->assertContains ('operator ' , $ rulesSubFieldNames );
106
+ $ this ->assertContains ('value ' , $ rulesSubFieldNames );
107
+ }
108
+
94
109
/**
95
110
* @testdox create returns an empty array if no rows are found
96
111
*/
@@ -109,7 +124,7 @@ public function testCreateReturnsAnEmptyArrayIfNoRowsAreFound()
109
124
public function testExpectedOptionsAreFetched ()
110
125
{
111
126
$ getOption = fn ($ option , $ default ) => $ option === 'options_external_content_sources ' ? '1 ' : $ default ;
112
- $ getOptions = fn ($ options ) => [ ' options_external_content_sources_0_taxonomies ' => ' 1 ' ] ;
127
+ $ getOptions = fn ($ options ) => $ this -> getTestAcfData () ;
113
128
$ wpService = new FakeWpService (['getOption ' => $ getOption , 'getOptions ' => $ getOptions ]);
114
129
115
130
@(new SourceConfigFactory ($ this ->getSchemaDataConfig (), $ wpService ))->create ();
@@ -125,6 +140,7 @@ public function testExpectedOptionsAreFetched()
125
140
'options_external_content_sources_0_source_typesense_host ' ,
126
141
'options_external_content_sources_0_source_typesense_port ' ,
127
142
'options_external_content_sources_0_source_typesense_collection ' ,
143
+ 'options_external_content_sources_0_rules ' ,
128
144
129
145
], $ wpService ->methodCalls ['getOptions ' ][0 ][0 ]);
130
146
@@ -135,31 +151,22 @@ public function testExpectedOptionsAreFetched()
135
151
'options_external_content_sources_0_taxonomies_0_hierarchical ' ,
136
152
137
153
], $ wpService ->methodCalls ['getOptions ' ][1 ][0 ]);
154
+
155
+ $ this ->assertEquals ([
156
+ 'options_external_content_sources_0_rules_0_property_path ' ,
157
+ 'options_external_content_sources_0_rules_0_operator ' ,
158
+ 'options_external_content_sources_0_rules_0_value ' ,
159
+
160
+ ], $ wpService ->methodCalls ['getOptions ' ][2 ][0 ]);
138
161
}
139
162
140
163
/**
141
164
* @testdox array of SourceConfigInterface objects are returned
142
165
*/
143
- public function test ()
166
+ public function testReturnsExpectedSourceConfigObjects ()
144
167
{
145
- $ getOption = fn ($ option , $ default ) => $ option === 'options_external_content_sources ' ? '1 ' : $ default ;
146
- $ getOptions = fn ($ options ) => [
147
- 'options_external_content_sources_0_post_type ' => 'test_post_type ' ,
148
- 'options_external_content_sources_0_automatic_import_schedule ' => 'test_schedule ' ,
149
- 'options_external_content_sources_0_taxonomies ' => '1 ' ,
150
- 'options_external_content_sources_0_source_type ' => 'test_source_type ' ,
151
- 'options_external_content_sources_0_source_json_file_path ' => 'test_json_file_path ' ,
152
- 'options_external_content_sources_0_source_typesense_api_key ' => 'test_api_key ' ,
153
- 'options_external_content_sources_0_source_typesense_protocol ' => 'test_protocol ' ,
154
- 'options_external_content_sources_0_source_typesense_host ' => 'test_host ' ,
155
- 'options_external_content_sources_0_source_typesense_port ' => 'test_port ' ,
156
- 'options_external_content_sources_0_source_typesense_collection ' => 'test_collection ' ,
157
- 'options_external_content_sources_0_taxonomies_0_from_schema_property ' => 'test_from_schema_property ' ,
158
- 'options_external_content_sources_0_taxonomies_0_singular_name ' => 'test_singular_name ' ,
159
- 'options_external_content_sources_0_taxonomies_0_name ' => 'test_name ' ,
160
- 'options_external_content_sources_0_taxonomies_0_hierarchical ' => true ,
161
- ];
162
- $ wpService = new FakeWpService (['getOption ' => $ getOption , 'getOptions ' => $ getOptions ]);
168
+ $ getOption = fn ($ option , $ default ) => $ option === 'options_external_content_sources ' ? '1 ' : $ default ;
169
+ $ wpService = new FakeWpService (['getOption ' => $ getOption , 'getOptions ' => fn ($ options ) => $ this ->getTestAcfData ()]);
163
170
164
171
$ sourceConfigs = (new SourceConfigFactory ($ this ->getSchemaDataConfig (), $ wpService ))->create ();
165
172
@@ -175,6 +182,9 @@ public function test()
175
182
$ this ->assertEquals ('test_from_schema_property ' , $ sourceConfigs [0 ]->getTaxonomies ()[0 ]->getFromSchemaProperty ());
176
183
$ this ->assertEquals ('test_singular_name ' , $ sourceConfigs [0 ]->getTaxonomies ()[0 ]->getSingularName ());
177
184
$ this ->assertEquals ('test_schema_type_test_from_schem ' , $ sourceConfigs [0 ]->getTaxonomies ()[0 ]->getName ());
185
+ $ this ->assertEquals ('test_property_path ' , $ sourceConfigs [0 ]->getFilterDefinition ()->getRuleSets ()[0 ]->getRules ()[0 ]->getPropertyPath ());
186
+ $ this ->assertEquals (Operator::EQUALS , $ sourceConfigs [0 ]->getFilterDefinition ()->getRuleSets ()[0 ]->getRules ()[0 ]->getOperator ());
187
+ $ this ->assertEquals ('test_value ' , $ sourceConfigs [0 ]->getFilterDefinition ()->getRuleSets ()[0 ]->getRules ()[0 ]->getValue ());
178
188
$ this ->assertEquals (true , $ sourceConfigs [0 ]->getTaxonomies ()[0 ]->isHierarchical ());
179
189
}
180
190
@@ -206,4 +216,33 @@ public function tryGetSchemaTypeFromPostType(string $postType): ?string
206
216
}
207
217
};
208
218
}
219
+
220
+ private function getAcfFields (): array
221
+ {
222
+ return json_decode (file_get_contents (__DIR__ . '/../../AcfFields/json/external-content-settings.json ' ), true );
223
+ }
224
+
225
+ private function getTestAcfData (): array
226
+ {
227
+ return [
228
+ 'options_external_content_sources_0_post_type ' => 'test_post_type ' ,
229
+ 'options_external_content_sources_0_automatic_import_schedule ' => 'test_schedule ' ,
230
+ 'options_external_content_sources_0_taxonomies ' => '1 ' ,
231
+ 'options_external_content_sources_0_source_type ' => 'test_source_type ' ,
232
+ 'options_external_content_sources_0_source_json_file_path ' => 'test_json_file_path ' ,
233
+ 'options_external_content_sources_0_source_typesense_api_key ' => 'test_api_key ' ,
234
+ 'options_external_content_sources_0_source_typesense_protocol ' => 'test_protocol ' ,
235
+ 'options_external_content_sources_0_source_typesense_host ' => 'test_host ' ,
236
+ 'options_external_content_sources_0_source_typesense_port ' => 'test_port ' ,
237
+ 'options_external_content_sources_0_source_typesense_collection ' => 'test_collection ' ,
238
+ 'options_external_content_sources_0_taxonomies_0_from_schema_property ' => 'test_from_schema_property ' ,
239
+ 'options_external_content_sources_0_taxonomies_0_singular_name ' => 'test_singular_name ' ,
240
+ 'options_external_content_sources_0_taxonomies_0_name ' => 'test_name ' ,
241
+ 'options_external_content_sources_0_taxonomies_0_hierarchical ' => true ,
242
+ 'options_external_content_sources_0_rules ' => '1 ' ,
243
+ 'options_external_content_sources_0_rules_0_property_path ' => 'test_property_path ' ,
244
+ 'options_external_content_sources_0_rules_0_operator ' => 'test_operator ' ,
245
+ 'options_external_content_sources_0_rules_0_value ' => 'test_value ' ,
246
+ ];
247
+ }
209
248
}
0 commit comments