Completed
Push — feature/EVO-7278-tracking-info... ( cdfec3...e99586 )
by
unknown
11:49 queued 05:41
created

DefaultControllerTest::testIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Tester to Controller
4
 */
5
namespace Graviton\AuditTrackingBundle\Tests\Controller;
6
7
use Doctrine\ODM\MongoDB\DocumentManager;
8
use Graviton\AuditTrackingBundle\Manager\StoreManager;
9
use Graviton\CoreBundle\Document\App;
10
use Graviton\TestBundle\Test\RestTestCase;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * Basic functional test for /core/app.
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class DefaultControllerTest extends RestTestCase
21
{
22
    /** Name to be used in test */
23
    const TEST_APP_ID = 'audit-id';
24
    
25
    /** @var  DocumentManager */
26
    private $documentManager;
27
28
    /**
29
     * Ensure a clean Db for test
30
     *
31
     * @return void
32
     */
33
    public function setUp()
34
    {
35
        // We only delete on first start up.
36
        if (!$this->documentManager) {
37
            $this->documentManager = $this->getContainer()->get('doctrine_mongodb.odm.default_document_manager');
38
39
            /** @var App $app */
40
            $app = $this->documentManager->find(get_class(new App()), self::TEST_APP_ID);
41
            if ($app) {
42
                $this->documentManager->remove($app);
43
                $this->documentManager->flush();
44
            }
45
        }
46
    }
47
48
    /**
49
     * @return \stdClass test object
50
     */
51
    private function getTestObj()
52
    {
53
        $new = new \stdClass();
54
        $new->id = self::TEST_APP_ID;
55
        $new->showInMenu = false;
56
        $new->order = 321;
57
        $new->name = new \stdClass();
58
        $new->name->en = 'audit en language name';
59
        $new->name->de = 'audit de language name';
60
        return $new;
61
    }
62
    
63
    /**
64
     * Insert a new APP element
65
     *
66
     * @return void
67
     */
68
    public function testInsertItem()
69
    {
70
        $new = $this->getTestObj();
71
72
        $client = static::createRestClient();
73
        $client->put('/core/app/'.self::TEST_APP_ID, $new);
74
        $response = $client->getResponse();
75
        $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
76
        
77
        // Lets check if the Audit Event was there and in header
78
        $header = $response->headers->get(StoreManager::AUDIT_HEADER_KEY);
79
        $this->assertNotEmpty($header, 'The expected audit header was not set as expected');
80
81
        // Get the data and hcek for a inserted new event
82
        $client = static::createRestClient();
83
        $client->request('GET', '/auditing/?eq(thread,string:'.$header.')');
84
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
85
        $results = $client->getResults();
86
87
        $this->assertEquals(1, count($results), 'By default, only one result. Only insert into DB');
88
        $event = $results[0];
89
        $this->assertEquals('insert', $event->{'action'});
90
        $this->assertEquals('collection', $event->{'type'});
91
        $this->assertEquals('App', $event->{'collectionName'});
92
        $this->assertEquals(self::TEST_APP_ID, $event->{'collectionId'});
93
    }
94
95
    /**
96
     * Update an APP element
97
     *
98
     * @return void
99
     */
100
    public function testUpdateItem()
101
    {
102
        $new = $this->getTestObj();
103
104
        $client = static::createRestClient();
105
        $client->put('/core/app/'.self::TEST_APP_ID, $new);
106
        $response = $client->getResponse();
107
        $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
108
109
        $client = static::createRestClient();
110
        $patchJson = json_encode(
111
            [
112
                [
113
                    'op' => 'replace',
114
                    'path' => '/name/en',
115
                    'value' => 'Test App audit Patched'
116
                ]
117
            ]
118
        );
119
        $client->request('PATCH', '/core/app/' . self::TEST_APP_ID, [], [], [], $patchJson);
120
        $response = $client->getResponse();
121
        $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
122
123
        // Lets check if the Audit Event was there and in header
124
        $header = $response->headers->get(StoreManager::AUDIT_HEADER_KEY);
125
        $this->assertNotEmpty($header, 'The expected audit header was not set as expected');
126
127
        // Get the data and hcek for a inserted new event
128
        $client = static::createRestClient();
129
        $client->request('GET', '/auditing/?eq(thread,string:'.$header.')');
130
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
131
        $results = $client->getResults();
132
133
        $this->assertEquals(1, count($results), 'By default, only one result. Only basic logs are active');
134
        $event = $results[0];
135
        $this->assertEquals('update', $event->{'action'});
136
        $this->assertEquals('collection', $event->{'type'});
137
        $this->assertEquals('App', $event->{'collectionName'});
138
        $this->assertEquals(self::TEST_APP_ID, $event->{'collectionId'});
139
    }
140
}
141