94ae428535ac9d67f98e6916ed218f8a6a22b5ec
[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                 )
49
50             ),
51             'returns' => 'array'
52         ), $smdArray['services']['Inventory.saveInventoryItem'], 'saveInventoryItem smd mismatch');
53         $this->assertEquals(array
54         (
55             'envelope' => 'JSON-RPC-2.0',
56             'transport' => 'POST',
57             'parameters' => array
58             (
59                 array
60                 (
61                     'type' => 'array',
62                     'optional' => false,
63                 )
64
65             ),
66             'returns' => 'array'
67         ), $smdArray['services']['Inventory.deleteInventoryItems']);
68     }
69
70     /**
71      * @group ServerTests
72      */
73     public function testGetAnonServiceMap()
74     {
75         // unset registry (and the user object)
76         Zend_Registry::_unsetInstance();
77
78         $smd = Tinebase_Server_Json::getServiceMap();
79         $smdArray = $smd->toArray();
80         $this->assertTrue(isset($smdArray['services']['Tinebase.ping']));
81     }
82
83     /**
84      * @group ServerTests
85      *
86      * @see  0011760: create smd from model definition
87      */
88     public function testHandleRequestForDynamicAPI()
89     {
90         // handle jsonkey check
91         $jsonkey = 'myawsomejsonkey';
92         $_SERVER['HTTP_X_TINE20_JSONKEY'] = $jsonkey;
93         $coreSession = Tinebase_Session::getSessionNamespace();
94         $coreSession->jsonKey = $jsonkey;
95
96         $server = new Tinebase_Server_Json();
97         $request = \Zend\Http\PhpEnvironment\Request::fromString(<<<EOS
98 POST /index.php?requestType=JSON HTTP/1.1\r
99 Host: localhost\r
100 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120824 Thunderbird/15.0 Lightning/1.7\r
101 Content-Type: application/json\r
102 X-Tine20-Transactionid: 18da265bc0eb66a36081bfd42689c1675ed68bab\r
103 X-Requested-With: XMLHttpRequest\r
104 Accept: */*\r
105 Referer: http://tine20.vagrant/\r
106 Accept-Encoding: gzip, deflate\r
107 Accept-Language: en-US,en;q=0.8,de-DE;q=0.6,de;q=0.4\r
108 \r
109 {"jsonrpc":"2.0","method":"Inventory.searchInventoryItems","params":{"filter":[], "paging":{}},"id":6}
110 EOS
111         );
112         ob_start();
113         $server->handle($request);
114         $out = ob_get_clean();
115         //echo $out;
116
117         $this->assertTrue(! empty($out), 'request should not be empty');
118         $this->assertNotContains('Not Authorised', $out);
119         $this->assertNotContains('Method not found', $out);
120         $this->assertNotContains('No Application Controller found', $out);
121         $this->assertNotContains('"error"', $out);
122         $this->assertNotContains('PHP Fatal error', $out);
123         $this->assertContains('"result"', $out);
124     }
125 }