Skip to content

Commit 238e986

Browse files
committed
EntityDisplayTest - Add coverage for VIEW mode
1 parent ccceefd commit 238e986

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

ext/search_kit/tests/phpunit/api/v4/SearchDisplay/EntityDisplayTest.php

+47-17
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@ public function setUpHeadless(): CiviEnvBuilder {
2222
->apply();
2323
}
2424

25-
public function testEntityDisplay() {
25+
public static function getDataModes(): array {
26+
return [
27+
['table'],
28+
['view'],
29+
];
30+
}
31+
32+
/**
33+
* @dataProvider getDataModes
34+
*/
35+
public function testEntityDisplay(string $dataMode) {
2636
$lastName = uniqid(__FUNCTION__);
2737

2838
$this->saveTestRecords('Contact', [
@@ -51,6 +61,7 @@ public function testEntityDisplay() {
5161
->addValue('label', 'MyNewEntity')
5262
->addValue('name', 'MyNewEntity')
5363
->addValue('settings', [
64+
'data_mode' => $dataMode,
5465
'columns' => [
5566
[
5667
'key' => 'id',
@@ -90,22 +101,33 @@ public function testEntityDisplay() {
90101
])
91102
->execute()->first();
92103

104+
$expectTypes = ['table' => 'BASE TABLE', 'view' => 'VIEW'];
105+
$this->assertEquals($expectTypes[$dataMode], \CRM_Core_BAO_SchemaHandler::getTableType('civicrm_sk_my_new_entity'));
106+
93107
$schema = \CRM_Core_DAO::executeQuery('DESCRIBE civicrm_sk_my_new_entity')->fetchAll();
94108
$this->assertCount(7, $schema);
95109
$this->assertEquals('_row', $schema[0]['Field']);
96-
$this->assertStringStartsWith('int', $schema[0]['Type']);
97-
$this->assertEquals('PRI', $schema[0]['Key']);
110+
// $this->assertStringStartsWith('int', $schema[0]['Type']);
111+
$this->assertMatchesRegularExpression('/^(int|bigint)/', $schema[0]['Type']);
112+
if ($dataMode === 'table') {
113+
$this->assertEquals('PRI', $schema[0]['Key']);
114+
}
98115

99-
// created_date and modified_date should be NULLable and have no default/extra
100-
$this->assertEmpty($schema[5]['Default']);
101-
$this->assertEmpty($schema[5]['Extra']);
102-
$this->assertEquals('YES', $schema[5]['Null']);
103-
$this->assertEmpty($schema[6]['Default']);
104-
$this->assertEmpty($schema[6]['Extra']);
105-
$this->assertEquals('YES', $schema[5]['Null']);
116+
if ($dataMode === 'table') {
117+
// created_date and modified_date should be NULLable and have no default/extra
118+
$this->assertEmpty($schema[5]['Default']);
119+
$this->assertEmpty($schema[5]['Extra']);
120+
$this->assertEquals('YES', $schema[5]['Null']);
121+
$this->assertEmpty($schema[6]['Default']);
122+
$this->assertEmpty($schema[6]['Extra']);
123+
$this->assertEquals('YES', $schema[5]['Null']);
124+
}
106125

107-
$rows = \CRM_Core_DAO::executeQuery('SELECT * FROM civicrm_sk_my_new_entity')->fetchAll();
108-
$this->assertCount(0, $rows);
126+
if ($dataMode === 'table') {
127+
// SQL table is not yet hydrated.
128+
$rows = \CRM_Core_DAO::executeQuery('SELECT * FROM civicrm_sk_my_new_entity')->fetchAll();
129+
$this->assertCount(0, $rows);
130+
}
109131

110132
$getFields = civicrm_api4('SK_MyNewEntity', 'getFields', ['loadOptions' => TRUE])->indexBy('name');
111133
$this->assertNotEmpty($getFields['prefix_id']['options'][1]);
@@ -160,7 +182,10 @@ public function testEntityDisplay() {
160182
$this->assertStringContainsString('SK_MyNewEntity', $e->getMessage());
161183
}
162184

163-
public function testEntityDisplayWithJoin() {
185+
/**
186+
* @dataProvider getDataModes
187+
*/
188+
public function testEntityDisplayWithJoin(string $dataMode) {
164189

165190
$lastName = uniqid(__FUNCTION__);
166191
$contacts = (array) $this->saveTestRecords('Individual', [
@@ -198,6 +223,7 @@ public function testEntityDisplayWithJoin() {
198223
->addValue('label', 'MyNewEntityWithJoin')
199224
->addValue('name', 'MyNewEntityWithJoin')
200225
->addValue('settings', [
226+
'data_mode' => $dataMode,
201227
// Additional column data will be filled in automatically
202228
// @see SKEntitySubscriber::formatFieldSpec
203229
'columns' => [
@@ -271,7 +297,10 @@ public function testEntityDisplayWithJoin() {
271297
$this->assertSame('Event_SK_MyNewEntityWithJoin_Contact_Participant_contact_id_01_event_id', $eventJoin[0]['alias']);
272298
}
273299

274-
public function testEntityWithSqlFunctions(): void {
300+
/**
301+
* @dataProvider getDataModes
302+
*/
303+
public function testEntityWithSqlFunctions(string $dataMode): void {
275304
$cids = $this->saveTestRecords('Individual', [
276305
'records' => [
277306
['first_name' => 'A', 'last_name' => 'A'],
@@ -324,6 +353,7 @@ public function testEntityWithSqlFunctions(): void {
324353
'saved_search_id.name' => 'Major_Donors_entity_test',
325354
'type' => 'entity',
326355
'settings' => [
356+
'data_mode' => $dataMode,
327357
'sort' => [
328358
['YEAR_receive_date', 'ASC'],
329359
],
@@ -361,9 +391,9 @@ public function testEntityWithSqlFunctions(): void {
361391
$schema = \CRM_Core_DAO::executeQuery('DESCRIBE civicrm_sk_major_donors_entity_test_db_entity1')->fetchAll();
362392
$this->assertCount(6, $schema);
363393
$this->assertEquals('_row', $schema[0]['Field']);
364-
$this->assertStringStartsWith('int', $schema[0]['Type']);
365-
$this->assertStringStartsWith('int', $schema[1]['Type']);
366-
$this->assertStringStartsWith('int', $schema[2]['Type']);
394+
$this->assertMatchesRegularExpression('/^(int|bigint)/', $schema[0]['Type']);
395+
$this->assertMatchesRegularExpression('/^(int|bigint)/', $schema[1]['Type']);
396+
$this->assertMatchesRegularExpression('/^(int|bigint)/', $schema[2]['Type']);
367397
$this->assertStringStartsWith('text', $schema[3]['Type']);
368398
$this->assertStringStartsWith('decimal', $schema[4]['Type']);
369399
$this->assertStringStartsWith('text', $schema[3]['Type']);

0 commit comments

Comments
 (0)