4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Action
;
22 use api\v
4\UnitTestCase
;
23 use Civi\Api4\Contact
;
28 class SqlExpressionTest
extends UnitTestCase
{
30 public function testSelectNull() {
31 Contact
::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
32 $result = Contact
::get()
33 ->addSelect('NULL AS nothing', 'NULL', 'NULL AS b*d char', 'first_name')
34 ->addWhere('first_name', '=', 'bob')
38 $this->assertNull($result['nothing']);
39 $this->assertNull($result['NULL']);
40 $this->assertNull($result['b_d_char']);
41 $this->assertEquals('bob', $result['first_name']);
42 $this->assertArrayNotHasKey('b*d char', $result);
45 public function testSelectNumbers() {
46 Contact
::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
47 $result = Contact
::get()
48 ->addSelect('first_name', 123, 45.678, '-55 AS neg')
49 ->addWhere('first_name', '=', 'bob')
53 $this->assertEquals('bob', $result['first_name']);
54 $this->assertEquals('123', $result['123']);
55 $this->assertEquals('-55', $result['neg']);
56 $this->assertEquals('45.678', $result['45_678']);
59 public function testSelectStrings() {
60 Contact
::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
61 $result = Contact
::get()
62 ->addSelect('first_name')
63 ->addSelect('"hello world" AS hi')
64 ->addSelect('"can\'t \"quote\"" AS quot')
65 ->addWhere('first_name', '=', 'bob')
69 $this->assertEquals('bob', $result['first_name']);
70 $this->assertEquals('hello world', $result['hi']);
71 $this->assertEquals('can\'t "quote"', $result['quot']);
74 public function testSelectAlias() {
77 ->addSelect('first_name AS bob')
80 catch (\API_Exception
$e) {
81 $msg = $e->getMessage();
83 $this->assertContains('alias', $msg);
86 ->addSelect('55 AS sort_name')
89 catch (\API_Exception
$e) {
90 $msg = $e->getMessage();
92 $this->assertContains('existing field name', $msg);
94 ->addSelect('55 AS ok_alias')