Merge pull request #17943 from jitendrapurohit/core-1906
[civicrm-core.git] / tests / phpunit / api / v4 / Action / SqlExpressionTest.php
CommitLineData
3176b04c
CW
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
3176b04c
CW
17 */
18
19
20namespace api\v4\Action;
21
22use api\v4\UnitTestCase;
23use Civi\Api4\Contact;
24
25/**
26 * @group headless
27 */
28class SqlExpressionTest extends UnitTestCase {
29
30 public function testSelectNull() {
31 Contact::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
32 $result = Contact::get()
19fde02c 33 ->addSelect('NULL AS nothing', 'NULL', 'NULL AS b*d char', 'first_name')
3176b04c
CW
34 ->addWhere('first_name', '=', 'bob')
35 ->setLimit(1)
36 ->execute()
37 ->first();
38 $this->assertNull($result['nothing']);
39 $this->assertNull($result['NULL']);
40 $this->assertNull($result['b_d_char']);
19fde02c 41 $this->assertEquals('bob', $result['first_name']);
3176b04c
CW
42 $this->assertArrayNotHasKey('b*d char', $result);
43 }
44
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')
50 ->setLimit(1)
51 ->execute()
52 ->first();
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']);
57 }
58
59 public function testSelectStrings() {
60 Contact::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
61 $result = Contact::get()
19fde02c 62 ->addSelect('first_name')
3176b04c
CW
63 ->addSelect('"hello world" AS hi')
64 ->addSelect('"can\'t \"quote\"" AS quot')
65 ->addWhere('first_name', '=', 'bob')
66 ->setLimit(1)
67 ->execute()
68 ->first();
19fde02c 69 $this->assertEquals('bob', $result['first_name']);
3176b04c
CW
70 $this->assertEquals('hello world', $result['hi']);
71 $this->assertEquals('can\'t "quote"', $result['quot']);
72 }
73
19fde02c
CW
74 public function testSelectAlias() {
75 try {
76 Contact::get()
77 ->addSelect('first_name AS bob')
78 ->execute();
79 }
80 catch (\API_Exception $e) {
81 $msg = $e->getMessage();
82 }
83 $this->assertContains('alias', $msg);
84 try {
85 Contact::get()
86 ->addSelect('55 AS sort_name')
87 ->execute();
88 }
89 catch (\API_Exception $e) {
90 $msg = $e->getMessage();
91 }
92 $this->assertContains('existing field name', $msg);
93 Contact::get()
94 ->addSelect('55 AS ok_alias')
95 ->execute();
96 }
97
3176b04c 98}