Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / SystemTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test class for System API - civicrm_system_*
14 *
15 * @package CiviCRM
16 * @group headless
17 */
18 class api_v3_SystemTest extends CiviUnitTestCase {
19
20 const TEST_CACHE_GROUP = 'SystemTest';
21 const TEST_CACHE_PATH = 'api/v3/system';
22
23 /**
24 * Sets up the fixture, for example, opens a network connection.
25 *
26 * This method is called before a test is executed.
27 */
28 protected function setUp() {
29 parent::setUp();
30 $this->useTransaction(TRUE);
31 }
32
33 /**
34 * Test system flush.
35 */
36 public function testFlush() {
37 // Note: this operation actually flushes several different caches; we don't
38 // check all of them -- just enough to make sure that the API is doing
39 // something
40
41 $this->assertTrue(NULL === Civi::cache()->get(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH)));
42
43 $data = 'abc';
44 Civi::cache()->set(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH), $data);
45
46 $this->assertEquals('abc', Civi::cache()->get(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH)));
47
48 $params = [];
49 $result = $this->callAPIAndDocument('system', 'flush', $params, __FUNCTION__, __FILE__, "Flush all system caches", 'Flush');
50
51 $this->assertTrue(NULL === Civi::cache()->get(CRM_Utils_Cache::cleanKey(self::TEST_CACHE_PATH)));
52 }
53
54 /**
55 * Test system log function.
56 */
57 public function testSystemLog() {
58 $this->callAPISuccess('system', 'log', ['level' => 'info', 'message' => 'We wish you a merry Christmas']);
59 $result = $this->callAPISuccess('SystemLog', 'getsingle', [
60 'sequential' => 1,
61 'message' => ['LIKE' => '%Chris%'],
62 ]);
63 $this->assertEquals($result['message'], 'We wish you a merry Christmas');
64 $this->assertEquals($result['level'], 'info');
65 }
66
67 /**
68 * Test system log function.
69 */
70 public function testSystemLogNoLevel() {
71 $this->callAPISuccess('system', 'log', ['message' => 'We wish you a merry Christmas', 'level' => 'alert']);
72 $result = $this->callAPISuccess('SystemLog', 'getsingle', [
73 'sequential' => 1,
74 'message' => ['LIKE' => '%Chris%'],
75 ]);
76 $this->assertEquals($result['message'], 'We wish you a merry Christmas');
77 $this->assertEquals($result['level'], 'alert');
78 }
79
80 public function testSystemGet() {
81 $result = $this->callAPISuccess('system', 'get', []);
82 $this->assertRegExp('/^[0-9]+\.[0-9]+\.[0-9a-z\-]+$/', $result['values'][0]['version']);
83 $this->assertEquals('UnitTests', $result['values'][0]['uf']);
84 }
85
86 /**
87 * @throws \CRM_Core_Exception
88 */
89 public function testSystemUTFMB8Conversion() {
90 $this->callAPISuccess('System', 'utf8conversion', []);
91 $table = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE civicrm_contact');
92 $table->fetch();
93 $this->assertStringEndsWith('DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC', $table->Create_Table);
94
95 $this->callAPISuccess('System', 'utf8conversion', ['is_revert' => 1]);
96 $table = CRM_Core_DAO::executeQuery('SHOW CREATE TABLE civicrm_contact');
97 $table->fetch();
98 $this->assertStringEndsWith('DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC', $table->Create_Table);
99 }
100
101 }