set json api functions parameter names
[tine20] / tests / tine20 / Tinebase / Server / JsonTests.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Tinebase
6  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
7  * @copyright   Copyright (c) 2016 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  */
10
11 /**
12  * Test class for Tinebase_Server_Json
13  * 
14  * @package     Tinebase
15  */
16 class Tinebase_Server_JsonTests extends TestCase
17 {
18     /**
19      * @group ServerTests
20      */
21     public function testGetServiceMap()
22     {
23         $smd = Tinebase_Server_Json::getServiceMap();
24         $smdArray = $smd->toArray();
25
26         $expectedFunctions = array(
27             'Inventory.searchInventoryItems',
28             'Inventory.saveInventoryItem',
29             'Inventory.deleteInventoryItems',
30             'Inventory.getInventoryItem',
31         );
32
33         foreach ($expectedFunctions as $function) {
34             $this->assertTrue(in_array($function, array_keys($smdArray['methods'])), 'fn not in methods: ' . $function);
35             $this->assertTrue(in_array($function, array_keys($smdArray['services'])), 'fn not in services: ' . $function);
36         }
37
38         $this->assertEquals(array
39         (
40             'envelope' => 'JSON-RPC-2.0',
41             'transport' => 'POST',
42             'parameters' => array
43             (
44                 array
45                 (
46                     'type' => 'array',
47                     'optional' => false,
48                     'name' => 'recordData'
49                 )
50
51             ),
52             'returns' => 'array'
53         ), $smdArray['services']['Inventory.saveInventoryItem'], 'saveInventoryItem smd mismatch');
54         $this->assertEquals(array
55         (
56             'envelope' => 'JSON-RPC-2.0',
57             'transport' => 'POST',
58             'parameters' => array
59             (
60                 array
61                 (
62                     'type' => 'array',
63                     'optional' => false,
64                     'name' => 'ids'
65                 )
66
67             ),
68             'returns' => 'array'
69         ), $smdArray['services']['Inventory.deleteInventoryItems']);
70     }
71
72     /**
73      * @group ServerTests
74      */
75     public function testGetAnonServiceMap()
76     {
77         // unset registry (and the user object)
78         Zend_Registry::_unsetInstance();
79
80         $smd = Tinebase_Server_Json::getServiceMap();
81         $smdArray = $smd->toArray();
82         $this->assertTrue(isset($smdArray['services']['Tinebase.ping']));
83     }
84
85     /**
86      * @group ServerTests
87      *
88      * @see  0011760: create smd from model definition
89      */
90     public function testHandleRequestForDynamicAPI()
91     {
92         // handle jsonkey check
93         $jsonkey = 'myawsomejsonkey';
94         $_SERVER['HTTP_X_TINE20_JSONKEY'] = $jsonkey;
95         $coreSession = Tinebase_Session::getSessionNamespace();
96         $coreSession->jsonKey = $jsonkey;
97
98         $server = new Tinebase_Server_Json();
99         $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
100 POST /index.php?requestType=JSON HTTP/1.1\r
101 Host: localhost\r
102 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7\r
103 Content-Type: application/json\r
104 X-Tine20-Transactionid: 18da265bc0eb66a36081bfd42689c1675ed68bab\r
105 X-Requested-With: XMLHttpRequest\r
106 Accept: */*\r
107 Referer: http://tine20.vagrant/\r
108 Accept-Encoding: gzip, deflate\r
109 Accept-Language: en-US,en;q=0.8,de-DE;q=0.6,de;q=0.4\r
110 \r
111 {"jsonrpc":"2.0","method":"Inventory.searchInventoryItems","params":{"filter":[], "paging":{}},"id":6}
112 EOS
113         );
114         ob_start();
115         $server->handle($request);
116         $out = ob_get_clean();
117         //echo $out;
118
119         $this->assertTrue(! empty($out), 'request should not be empty');
120         $this->assertNotContains('Not Authorised', $out);
121         $this->assertNotContains('Method not found', $out);
122         $this->assertNotContains('No Application Controller found', $out);
123         $this->assertNotContains('"error"', $out);
124         $this->assertNotContains('PHP Fatal error', $out);
125         $this->assertContains('"result"', $out);
126     }
127 }