dev/core#147 Use location type m/c name as table alias instead of label
authordeb.monish <monish.deb@jmaconsulting.biz>
Tue, 29 May 2018 02:52:49 +0000 (08:22 +0530)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Fri, 1 Jun 2018 17:01:22 +0000 (22:31 +0530)
CRM/Contact/BAO/Query.php
CRM/Contact/Selector.php
CRM/Core/BAO/Mapping.php
tests/phpunit/CRM/Contact/SelectorTest.php

index c7b2bf851a60f9fcf02735536dbaa573d47b9369..a4618da916a8c646a4b38f2aaf7f1d2da539b575 100644 (file)
@@ -1013,7 +1013,7 @@ class CRM_Contact_BAO_Query {
       return;
     }
 
-    $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
+    $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
     $processed = array();
     $index = 0;
 
@@ -2374,7 +2374,7 @@ class CRM_Contact_BAO_Query {
       list($tbName, $fldName) = explode(".", $where);
 
       //get the location name
-      $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
+      $locationType = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
       $specialFields = array('email', 'im', 'phone', 'openid', 'phone_ext');
       if (in_array($locType[0], $specialFields)) {
         //hack to fix / special handing for phone_ext
@@ -2800,7 +2800,7 @@ class CRM_Contact_BAO_Query {
           if ($locationTypeName) {
             //we have a join on an location table - possibly in conjunction with search builder - CRM-14263
             $parts = explode('-', $name);
-            $locationTypes = CRM_Core_BAO_Address::buildOptions('location_type_id', 'get');
+            $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
             foreach ($locationTypes as $locationTypeID => $locationType) {
               if ($parts[0] == str_replace(' ', '_', $locationType)) {
                 $locationID = $locationTypeID;
index 2efc534d0f7dbacd545647fe39d0c41596087796..731f78c69d6eacdc41d6f5ae5059ddf6a7800154 100644 (file)
@@ -416,7 +416,7 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
           ),
         );
 
-        $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
+        $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
 
         foreach ($this->_fields as $name => $field) {
           if (!empty($field['in_selector']) &&
@@ -492,7 +492,16 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se
           if (trim($phoneType) && !is_numeric($phoneType) && strtolower($phoneType) != $fld) {
             $title .= "-{$phoneType}";
           }
-          $title .= " ($loc)";
+          // fetch Location type label from name as $loc, which will be later used in column header
+          $title .= sprintf(" (%s)",
+            CRM_Core_PseudoConstant::getLabel(
+              'CRM_Core_DAO_Address',
+              'location_type_id',
+              CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_Address', 'location_type_id', $loc)
+            )
+          );
+          // use field name instead of table alias
+          $prop = $fld;
         }
         elseif (isset($this->_query->_fields[$prop]) && isset($this->_query->_fields[$prop]['title'])) {
           $title = $this->_query->_fields[$prop]['title'];
index 65a8ba51f53ed4b8ffb3f8160202c92fdfce541b..1764cd572f1a7fe2ecfbefeea1835f4af4414315 100644 (file)
@@ -1130,7 +1130,7 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping {
       return $fields;
     }
 
-    $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
+    $locationTypes = CRM_Core_DAO_Address::buildOptions('location_type_id', 'validate');
     foreach ($params['mapper'] as $key => $value) {
       foreach ($value as $k => $v) {
         if (isset($v[1])) {
index aabf86b7aec1171fe003847268e5289726b6fc29..b847498a1c1c95f12b54de85152c237c033735ae 100644 (file)
@@ -274,6 +274,67 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase {
     $searchOBJ->contactIDQuery($params, '1_u');
   }
 
+  /**
+   * Test the Search Builder using Non ASCII location type for email filter
+   */
+  public function testSelectorQueryOnNonASCIIlocationType() {
+    $contactID = $this->individualCreate();
+    $locationType = $this->locationTypeCreate([
+      'name' => 'Non ASCII Location Type',
+      'display_name' => 'Дом Location type',
+      'vcard_name' => 'Non ASCII Location Type',
+      'is_active' => 1,
+    ]);
+    $this->callAPISuccess('Email', 'create', [
+      'contact_id' => $contactID,
+      'location_type_id' => $locationType->id,
+      'email' => 'test@test.com',
+    ]);
+
+    $selector = new CRM_Contact_Selector(
+      'CRM_Contact_Selector',
+      ['email' => ['IS NOT NULL' => 1]],
+      [[
+        0 => 'email-' . $locationType->id,
+        1 => 'IS NOT NULL',
+        2 => NULL,
+        3 => 1,
+        4 => 0,
+      ]],
+      [
+        'contact_type' => 1,
+        'contact_sub_type' => 1,
+        'sort_name' => 1,
+        'location' => [
+          'Non ASCII Location Type' => [
+            'location_type' => $locationType->id,
+            'email' => 1,
+          ],
+        ],
+      ],
+      CRM_Core_Action::NONE,
+      NULL,
+      FALSE,
+      'builder'
+    );
+
+    $sql = $selector->getQueryObject()->query();
+
+    $expectedQuery = [
+      0 => "SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, `Non_ASCII_Location_Type-location_type`.id as `Non_ASCII_Location_Type-location_type_id`, `Non_ASCII_Location_Type-location_type`.name as `Non_ASCII_Location_Type-location_type`, `Non_ASCII_Location_Type-email`.id as `Non_ASCII_Location_Type-email_id`, `Non_ASCII_Location_Type-email`.email as `Non_ASCII_Location_Type-email`",
+      // @TODO these FROM clause doesn't matches due to extra spaces or special character
+      2 => "WHERE  (  ( LOWER(`Non_ASCII_Location_Type-email`.email) IS NOT NULL )  )  AND (contact_a.is_deleted = 0)",
+    ];
+    foreach ($expectedQuery as $index => $queryString) {
+      $this->assertEquals($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
+    }
+
+    $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, TRUE, NULL);
+    $this->assertEquals(1, count($rows));
+    $this->assertEquals($contactID, key($rows));
+    $this->assertEquals('test@test.com', $rows[$contactID]['Non_ASCII_Location_Type-email']);
+  }
+
   /**
    * Test if custom table is added in from clause when
    * search results are ordered by a custom field.