Completed
Branch test-environment (ae587d)
by Julius
02:23
created

SettingsControllerTest::testGetAppOrderNoUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace OCA\AppOrder\Tests\Unit\Controller;
4
5
use OCP\IRequest;
6
use OCP\AppFramework\Http\DataResponse;
7
use OCP\AppFramework\Http;
8
use \OCA\AppOrder\Service;
9
use \OCA\AppOrder\AppInfo\Application;
10
use \OCA\AppOrder\Controller;
11
12
class SettingsControllerTest extends \Test\TestCase {
13
14
  private $container;
15
  private $request;
16
  private $service;
17
  private $navigationManager;
18
  private $userId;
19
  private $appName;
20
  private $controller;
21
  private $config;
22
  public function setUp (){
23
24
    parent::setUp();
25
26
    $app = new Application();
27
    $this->container = $app->getContainer();
28
    $this->request = $this->getMockBuilder('OCP\IRequest')
29
      ->disableOriginalConstructor()
30
      ->getMock();
31
    $this->config = $this->getMockBuilder('OCP\IConfig')
32
      ->disableOriginalConstructor()
33
      ->getMock();
34
    $this->service = $this->getMockBuilder('\OCA\AppOrder\Service\ConfigService')
35
      ->disableOriginalConstructor()
36
      ->getMock();
37
    $this->navigationManager = $this->getMockBuilder('\OCP\INavigationManager')->setMethods(['getAll','add','setActiveEntry'])
38
      ->disableOriginalConstructor()
39
      ->getMock();
40
    $this->userId = 'admin';
41
    $this->appName = 'apporder';
42
    $this->controller = new \OCA\AppOrder\Controller\SettingsController(
43
      $this->appName, $this->request, $this->service, $this->navigationManager,
44
      $this->userId
45
    );
46
47
  }
48
49
  public function test() {
50
  /*  $expected = ['hi'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
    $this->service->expects($this->once())
52
      ->method('getAll')
53
      ->will($this->returnValue($expected));
54
    $response = $this->controller->index();
55
    $this->assertEquals($expected, $response->getData());
56
  $this->assertTrue($response instanceof DataResponse);*/
57
  }
58
59
  public function testAdminIndex() {
60
    $nav_custom = ['/app/calendar/', '/app/tasks/'];
61
    $nav_oc = [
62
      ['href' => '/app/files/', 'name' => 'Files'],
63
      ['href' => '/app/calendar/', 'name' => 'Calendar'],
64
      ['href' => '/app/tasks/', 'name' => 'Tasks'],
65
    ];
66
    $nav_final = [
67
      '/app/calendar/' => $nav_oc[1], '/app/tasks/' => $nav_oc[2], '/app/files/' => $nav_oc[0]
68
      ];
69
    $this->service->expects($this->once())
70
      ->method('getAppValue')
71
      ->with('order')
72
      ->will($this->returnValue(json_encode($nav_custom)));
73
    $this->navigationManager->expects($this->once())
74
      ->method('getAll')
75
      ->will($this->returnValue($nav_oc));
76
77
    $result = $this->controller->adminIndex(); 
78
    $expected = new \OCP\AppFramework\Http\TemplateResponse(
79
      $this->appName, 
80
      'admin',
81
      ["nav" => $nav_final],
82
      'blank'
83
    );
84
    $this->assertEquals($expected, $result);
85
  }
86
87
  public function testMatchOrder() {
88
    $nav = [
89
      ['href' => '/app/files/', 'name' => 'Files'],
90
      ['href' => '/app/calendar/', 'name' => 'Calendar'],
91
      ['href' => '/app/tasks/', 'name' => 'Tasks'],
92
    ];
93
    $order = ['/app/calendar/', '/app/tasks/'];
94
    $result = $this->controller->matchOrder($nav, $order);
95
    $expected = [
96
      '/app/calendar/' => ['href' => '/app/calendar/', 'name' => 'Calendar'],
97
      '/app/tasks/' => ['href' => '/app/tasks/', 'name' => 'Tasks'],
98
      '/app/files/' => ['href' => '/app/files/', 'name' => 'Files'],
99
    ];
100
    $this->assertEquals($expected, $result);
101
  }
102
103
  public function testGetAppOrder() {
104
    $nav_system= ['/app/calendar/', '/app/tasks/'];
105
    $nav_user = ['/app/files/', '/app/calendar/', '/app/tasks/'];
106
    $this->service->expects($this->once())
107
      ->method('getAppValue')
108
      ->with('order')
109
      ->will($this->returnValue(json_encode($nav_system)));
110
    $this->service->expects($this->once())
111
      ->method('getUserValue')
112
      ->with('order', $this->userId)
113
      ->will($this->returnValue(json_encode($nav_user)));
114
    $result = $this->controller->getAppOrder();
115
    $this->assertEquals(json_encode($nav_user), $result);
116
  }
117
  public function testGetAppOrderNoUser() {
118
    $nav_system= ['/app/calendar/', '/app/tasks/'];
119
    $nav_user = '';
120
    $this->service->expects($this->once())
121
      ->method('getAppValue')
122
      ->with('order')
123
      ->will($this->returnValue(json_encode($nav_system)));
124
    $this->service->expects($this->once())
125
      ->method('getUserValue')
126
      ->with('order', $this->userId)
127
      ->will($this->returnValue($nav_user));
128
    $result = $this->controller->getAppOrder();
129
    $this->assertEquals(json_encode($nav_system), $result);
130
  }
131
}
132