Merge branch '2015.11-develop' into 2016.11
[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         $this->assertEquals(array
72         (
73             'envelope' => 'JSON-RPC-2.0',
74             'transport' => 'POST',
75             'parameters' => array
76             (
77                 array
78                 (
79                     'type' => 'array',
80                     'optional' => false,
81                     'name' => 'filter'
82                 ),
83                 array
84                 (
85                     'type' => 'array',
86                     'optional' => false,
87                     'name' => 'paging'
88                 )
89             ),
90             'returns' => 'array'
91         ), $smdArray['services']['Inventory.searchInventoryItems']);
92     }
93
94     /**
95      * @group ServerTests
96      */
97     public function testGetAnonServiceMap()
98     {
99         // unset registry (and the user object)
100         Zend_Registry::_unsetInstance();
101
102         $smd = Tinebase_Server_Json::getServiceMap();
103         $smdArray = $smd->toArray();
104         $this->assertTrue(isset($smdArray['services']['Tinebase.ping']));
105     }
106
107     /**
108      * @group ServerTests
109      *
110      * @see  0011760: create smd from model definition
111      */
112     public function testHandleRequestForDynamicAPI()
113     {
114         // handle jsonkey check
115         $jsonkey = 'myawsomejsonkey';
116         $_SERVER['HTTP_X_TINE20_JSONKEY'] = $jsonkey;
117         $coreSession = Tinebase_Session::getSessionNamespace();
118         $coreSession->jsonKey = $jsonkey;
119
120         $server = new Tinebase_Server_Json();
121         $request = \Zend\Http\PhpEnvironment\Request::fromString(
122 'POST /index.php?requestType=JSON HTTP/1.1' . "\r\n"
123 . 'Host: localhost' . "\r\n"
124 . 'User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7' . "\r\n"
125 . 'Content-Type: application/json' . "\r\n"
126 . 'X-Tine20-Transactionid: 18da265bc0eb66a36081bfd42689c1675ed68bab' . "\r\n"
127 . 'X-Requested-With: XMLHttpRequest' . "\r\n"
128 . 'Accept: */*' . "\r\n"
129 . 'Referer: http://tine20.vagrant/' . "\r\n"
130 . 'Accept-Encoding: gzip, deflate' . "\r\n"
131 . 'Accept-Language: en-US,en;q=0.8,de-DE;q=0.6,de;q=0.4' . "\r\n"
132 . "\r\n"
133 . '{"jsonrpc":"2.0","method":"Inventory.searchInventoryItems","params":{"filter":[], "paging":{}},"id":6}' . "\r\n"
134         );
135         ob_start();
136         $server->handle($request);
137         $out = ob_get_clean();
138         //echo $out;
139
140         $this->assertTrue(! empty($out), 'request should not be empty');
141         $this->assertNotContains('Not Authorised', $out);
142         $this->assertNotContains('Method not found', $out);
143         $this->assertNotContains('No Application Controller found', $out);
144         $this->assertNotContains('"error"', $out);
145         $this->assertNotContains('PHP Fatal error', $out);
146         $this->assertContains('"result"', $out);
147     }
148 }