Skip to content

Commit 633b4a4

Browse files
committed
- update clone repeater tests
1 parent 477aff4 commit 633b4a4

File tree

1 file changed

+107
-58
lines changed

1 file changed

+107
-58
lines changed

tests/functional/TestCloneWithRepeaterCest.php

Lines changed: 107 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class TestCloneFieldsCest {
3+
class TestCloneWithRepeaterCest {
44

55
public function _before( FunctionalTester $I, \Codeception\Scenario $scenario ) {
66

@@ -16,11 +16,8 @@ public function _before( FunctionalTester $I, \Codeception\Scenario $scenario )
1616

1717
}
1818

19-
public function testClonedFieldGroupAppliedAsInterface( FunctionalTester $I ) {
20-
21-
$I->wantTo( 'Test Cloned Field Groups are applied as Interface' );
22-
23-
$query = '
19+
public function _getQuery() {
20+
return '
2421
query GetType($type: String!) {
2522
__type(name: $type) {
2623
name
@@ -41,19 +38,35 @@ interfaces {
4138
}
4239
}
4340
';
41+
}
42+
43+
public function _executeQuery( FunctionalTester $I, $query = null, $variables = null ) {
44+
45+
if ( null === $query ) {
46+
$query = $this->_getQuery();
47+
}
4448

4549
$I->haveHttpHeader( 'Content-Type', 'application/json' );
4650
$I->sendPost( '/graphql', json_encode([
4751
'query' => $query,
48-
'variables' => [
49-
'type' => 'Flowers'
50-
]
52+
'variables' => $variables
5153
]));
5254

5355
$I->seeResponseCodeIs( 200 );
5456
$I->seeResponseIsJson();
5557
$response = $I->grabResponse();
5658
$response = json_decode( $response, true );
59+
return $response;
60+
}
61+
62+
public function testImportedFieldGroupsShowInSchema( FunctionalTester $I ) {
63+
64+
$I->wantTo( 'Test the Imported Field Groups are shown in the Schema' );
65+
66+
$variables = [
67+
'type' => 'Flowers'
68+
];
69+
$response = $this->_executeQuery( $I, null, $variables );
5770

5871
$I->assertNotEmpty( $response['data']['__type']['fields'] );
5972
$I->assertNotEmpty( $response['data']['__type']['interfaces'] );
@@ -74,11 +87,35 @@ interfaces {
7487
$I->assertTrue( in_array( 'avatar', $fields, true ) );
7588
$I->assertTrue( in_array( 'range', $fields, true ) );
7689

90+
91+
92+
$query = '
93+
query GetPageWithPlants($databaseId: ID!) {
94+
page(id:$databaseId idType:DATABASE_ID) {
95+
id
96+
title
97+
...on WithAcfPlants {
98+
plants {
99+
name
100+
clonedRepeater {
101+
notClonedRepeater {
102+
anotherName
103+
}
104+
}
105+
notClonedRepeater {
106+
anotherName
107+
}
108+
}
109+
}
110+
}
111+
}
112+
';
113+
77114
$I->haveHttpHeader( 'Content-Type', 'application/json' );
78115
$I->sendPost( '/graphql', json_encode([
79116
'query' => $query,
80117
'variables' => [
81-
'type' => 'Plants'
118+
'databaseId' => 0
82119
]
83120
]));
84121

@@ -87,6 +124,21 @@ interfaces {
87124
$response = $I->grabResponse();
88125
$response = json_decode( $response, true );
89126

127+
$I->wantTo( 'Test that a query against the field group is valid' );
128+
129+
// Validate that the query above was valid, returned data, and no errors
130+
$I->assertNotEmpty( $response['data'] );
131+
$I->assertArrayNotHasKey( 'errors', $response );
132+
}
133+
134+
function testClonedFieldsAppliedAsInterfaces( FunctionalTester $I ) {
135+
$I->wantTo( 'Test Cloned Field Groups are applied as interfaces' );
136+
137+
$variables = [
138+
'type' => 'Plants'
139+
];
140+
$response = $this->_executeQuery( $I, null, $variables );
141+
90142
$I->assertNotEmpty( $response['data']['__type']['fields'] );
91143
$I->assertNotEmpty( $response['data']['__type']['interfaces'] );
92144

@@ -111,11 +163,33 @@ interfaces {
111163
$I->assertTrue( in_array( 'avatar', $fields, true ) );
112164
$I->assertTrue( in_array( 'range', $fields, true ) );
113165

166+
}
167+
168+
public function testClonedRepeaterFieldShowsInSchema(FunctionalTester $I ) {
169+
170+
$I->wantTo( 'Test Cloned Repeater field shows in the Schema' );
171+
172+
$variables = [
173+
'type' => 'Plants'
174+
];
175+
$response = $this->_executeQuery( $I, null, $variables );
176+
177+
$I->assertNotEmpty( $response['data']['__type']['fields'] );
178+
$I->assertNotEmpty( $response['data']['__type']['interfaces'] );
179+
180+
$fields = array_map( static function( $field ) {
181+
return $field['name'];
182+
}, $response['data']['__type']['fields'] );
183+
184+
$interfaces = array_map( static function( $interface ) {
185+
return $interface['name'];
186+
}, $response['data']['__type']['interfaces'] );
187+
114188
// This field used to cause things to explode so this test ensures things
115189
// are working properly when cloning field groups that contain repeater fields
116190
$I->assertTrue( in_array( 'landMineRepeater', $fields, true ) );
117191

118-
$field = $this->findField( 'landMineRepeater', $fields );
192+
$field = $this->_findField( 'landMineRepeater', $response['data']['__type']['fields'] );
119193

120194
$I->assertSame( 'LIST', $field['type']['kind'] );
121195
$I->assertSame( 'FlowersLandMineRepeater', $field['type']['ofType']['name'] );
@@ -124,63 +198,38 @@ interfaces {
124198
$I->assertTrue( in_array( 'clonedRepeater', $fields, true ) );
125199

126200
// Find the clonedRepeaterField
127-
$field = $this->findField( 'clonedRepeater', $fields );
201+
$field = $this->_findField( 'clonedRepeater', $response['data']['__type']['fields'] );
128202

129203
$I->assertSame( 'OBJECT', $field['type']['kind'] );
130-
$I->assertNull( $field['type']['ofType']['name'] );
204+
$I->assertNull( $field['type']['ofType'] );
131205
$I->assertSame( 'PlantsClonedRepeater', $field['type']['name'] );
132206

133207
// Find the cloneRoots field (clone of the "flowers" field group, but with "prefix_name" set)
134-
$field = $this->findField( 'cloneRoots', $fields );
208+
$field = $this->_findField( 'cloneRoots', $response['data']['__type']['fields'] );
135209

136210
$I->assertSame( 'OBJECT', $field['type']['kind'] );
137-
$I->assertNull( $field['type']['ofType']['name'] );
211+
$I->assertNull( $field['type']['ofType'] );
138212
$I->assertSame( 'PlantsCloneRoots', $field['type']['name'] );
139-
140-
$query = '
141-
query GetPageWithPlants($databaseId: ID!) {
142-
page(id:$databaseId idType:DATABASE_ID) {
143-
id
144-
title
145-
...on WithAcfPlants {
146-
plants {
147-
name
148-
clonedRepeater {
149-
notClonedRepeater {
150-
anotherName
151-
}
152-
}
153-
notClonedRepeater {
154-
anotherName
155-
}
156-
}
157-
}
158-
}
159-
}
160-
';
161-
162-
$I->haveHttpHeader( 'Content-Type', 'application/json' );
163-
$I->sendPost( '/graphql', json_encode([
164-
'query' => $query,
165-
'variables' => [
166-
'databaseId' => 0
167-
]
168-
]));
169-
170-
$I->seeResponseCodeIs( 200 );
171-
$I->seeResponseIsJson();
172-
$response = $I->grabResponse();
173-
$response = json_decode( $response, true );
174-
175-
// Validate that the query above was valid, returned data, and no errors
176-
$I->assertNotEmpty( $response['data'] );
177-
$I->assertArrayNotHasKey( 'errors', $response );
178213
}
179214

180-
public function findField( $name, $fields ) {
181-
return array_filter( array_map( static function( $field ) use ( $name ) {
182-
return $field['name'] === $name ? $field : null;
183-
}, $fields ) );
215+
/**
216+
* Given a field name, find the field associated with that name
217+
*
218+
* @param string $name The name of the field to find
219+
* @param array $fields The array of fields to search
220+
*
221+
* @return ?array Returns the field with the name being searched for
222+
*/
223+
public function _findField( string $name, array $fields ): ?array {
224+
$found_field = null;
225+
if ( ! empty( $fields ) ) {
226+
foreach ( $fields as $field ) {
227+
if ( isset( $field['name'] ) && $field['name'] === $name ) {
228+
$found_field = $field;
229+
}
230+
}
231+
}
232+
return $found_field;
184233
}
185234

186235

0 commit comments

Comments
 (0)