Merge pull request #16162 from colemanw/contactFields
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 1 Jan 2020 03:37:25 +0000 (16:37 +1300)
committerGitHub <noreply@github.com>
Wed, 1 Jan 2020 03:37:25 +0000 (16:37 +1300)
Add contact type to schema to allow GetFields filtering

1  2 
CRM/Core/CodeGen/Specification.php
tests/phpunit/api/v4/Action/GetExtraFieldsTest.php

Simple merge
index 2fe52cddc46691ca48386167b6dfed9c2488ab42,60126b16de6b44ce767b834d5f19852919f6d744..fbdbd0c5792f6ad9a04d221331c753c13fd4b6c6
@@@ -30,31 -29,26 +30,40 @@@ use Civi\Api4\Contact
   */
  class GetExtraFieldsTest extends UnitTestCase {
  
-   public function testBAOFieldsWillBeReturned() {
-     $returnedFields = Contact::getFields()
-       ->execute()
-       ->getArrayCopy();
+   public function testGetFieldsByContactType() {
+     $getFields = Contact::getFields()->setCheckPermissions(FALSE)->addSelect('name')->setIncludeCustom(FALSE);
  
-     $baseFields = \CRM_Contact_BAO_Contact::fields();
-     $baseFieldNames = array_column($baseFields, 'name');
-     $returnedFieldNames = array_column($returnedFields, 'name');
-     $notReturned = array_diff($baseFieldNames, $returnedFieldNames);
+     $baseFields = array_column(\CRM_Contact_BAO_Contact::fields(), 'name');
+     $returnedFields = $getFields->execute()->column('name');
+     $notReturned = array_diff($baseFields, $returnedFields);
  
+     // With no contact_type specified, all fields should be returned
      $this->assertEmpty($notReturned);
+     $individualFields = $getFields->setValues(['contact_type' => 'Individual'])->execute()->column('name');
+     $this->assertNotContains('sic_code', $individualFields);
+     $this->assertNotContains('contact_type', $individualFields);
+     $this->assertContains('first_name', $individualFields);
+     $organizationFields = $getFields->setValues(['contact_type' => 'Organization'])->execute()->column('name');
+     $this->assertContains('sic_code', $organizationFields);
+     $this->assertNotContains('contact_type', $organizationFields);
+     $this->assertNotContains('first_name', $organizationFields);
+     $this->assertNotContains('household_name', $organizationFields);
    }
  
 +  public function testGetOptionsAddress() {
 +    $getFields = Address::getFields()->setCheckPermissions(FALSE)->addWhere('name', '=', 'state_province_id')->setLoadOptions(TRUE);
 +
 +    $usOptions = $getFields->setValues(['country_id' => 1228])->execute()->first();
 +
 +    $this->assertContains('Alabama', $usOptions['options']);
 +    $this->assertNotContains('Alberta', $usOptions['options']);
 +
 +    $caOptions = $getFields->setValues(['country_id' => 1039])->execute()->first();
 +
 +    $this->assertNotContains('Alabama', $caOptions['options']);
 +    $this->assertContains('Alberta', $caOptions['options']);
 +  }
 +
  }