ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilChatroomUserTest.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
13  protected $ilUserMock;
14 
18  protected $ilChatroomMock;
19 
23  protected $user;
24 
25  protected function setUp()
26  {
27  if(defined('ILIAS_PHPUNIT_CONTEXT'))
28  {
29  include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
30  ilUnitUtil::performInitialisation();
31  }
32  else
33  {
34  chdir(dirname(__FILE__));
35  chdir('../../../');
36  }
37 
38  require_once './Modules/Chatroom/classes/class.ilChatroomUser.php';
39  //require_once 'Services/User/classes/class.ilObjUser.php';
40  $this->ilUserMock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->setMethods(
41  array('getId', 'isAnonymous', 'getLogin', 'getPublicName', 'getFirstname', 'getLastname')
42  )->getMock();
43  $this->ilChatroomMock = $this->getMockBuilder('ilChatroom')->disableOriginalConstructor()->setMethods(
44  array('getRoomId', 'getSetting')
45  )->getMock();
46 
47  $this->user = new ilChatroomUser($this->ilUserMock, $this->ilChatroomMock);
48  }
49 
50  public function testConstructor()
51  {
52  $this->assertInstanceOf('ilChatroomUser', $this->user);
53  }
54 
55  public function testGetUserIdIfNotAnonymous()
56  {
57  $userId = 6;
58 
59  $this->ilUserMock->expects($this->once())->method('getId')->will($this->returnValue($userId));
60  $this->ilUserMock->expects($this->once())->method('isAnonymous')->will($this->returnValue(false));
61 
62  $this->assertEquals($userId, $this->user->getUserId());
63  }
64 
66  {
67  $userId = 6;
68  $roomId = 99;
69 
70  $this->ilUserMock->expects($this->once())->method('getId')->will($this->returnValue($userId));
71  $this->ilUserMock->expects($this->once())->method('isAnonymous')->will($this->returnValue(true));
72 
73  $this->ilChatroomMock->expects($this->any())->method('getRoomId')->will($this->returnValue($roomId));
74 
75  $_SESSION['chat'] = array(
76  $roomId => array(
77  'user_id' => $userId,
78  ),
79  );
80 
81  $this->assertEquals($userId, $this->user->getUserId());
82  }
83 
85  {
86  $this->ilUserMock->expects($this->once())->method('getId')->will($this->returnValue(null));
87  $this->ilUserMock->expects($this->once())->method('isAnonymous')->will($this->returnValue(true));
88 
89  $this->ilChatroomMock->expects($this->any())->method('getRoomId')->will($this->returnValue(99));
90 
91  $this->assertNotNull($this->user->getUserId());
92  }
93 
99  public function testSetUsername($username, $expected)
100  {
101  $this->user->setUsername($username);
102  $this->assertEquals($expected, $this->user->getUsername());
103  }
104 
105  public function testGetUsernameFromSession()
106  {
107  $username = 'username';
108  $roomId = 99;
109  $_SESSION['chat'][$roomId]['username'] = $username;
110 
111  $this->ilChatroomMock->expects($this->any())->method('getRoomId')->will($this->returnValue(99));
112 
113  $this->assertEquals($username, $this->user->getUsername());
114  }
115 
121  {
122  $username = 'login';
123  $roomId = 99;
124  $_SESSION['chat'][$roomId]['username'] = ''; // Fix missing key warning
125 
126  $this->ilUserMock->expects($this->once())->method('getLogin')->will($this->returnValue($username));
127  $this->ilChatroomMock->expects($this->any())->method('getRoomId')->will($this->returnValue($roomId));
128 
129  $this->assertEquals($username, $this->user->getUsername());
130  }
131 
132  public function testBuildAnonymousName()
133  {
134  $this->ilChatroomMock->expects($this->any())->method('getSetting')->will($this->returnValue('#_anonymous'));
135 
136  $firstName = $this->user->buildAnonymousName();
137  $secondName = $this->user->buildAnonymousName();
138 
139  $this->assertNotEquals($firstName, $secondName);
140  }
141 
142  public function testBuildLogin()
143  {
144  $username = 'username';
145  $this->ilUserMock->expects($this->once())->method('getLogin')->will($this->returnValue($username));
146 
147  $this->assertEquals($username, $this->user->buildLogin());
148  }
149 
150  public function testBuildFullname()
151  {
152  $fullname = 'John Doe';
153  $this->ilUserMock->expects($this->once())->method('getPublicName')->will($this->returnValue($fullname));
154 
155  $this->assertEquals($fullname, $this->user->buildFullname());
156  }
157 
158  public function testBuildShortname()
159  {
160  $firstname = 'John';
161  $lastname = 'Doe';
162  $this->ilUserMock->expects($this->once())->method('getFirstname')->will($this->returnValue($firstname));
163  $this->ilUserMock->expects($this->once())->method('getLastname')->will($this->returnValue($lastname));
164 
165  $this->assertEquals('J. Doe', $this->user->buildShortname());
166  }
167 
169  {
170  $this->ilUserMock->expects($this->any())->method('isAnonymous')->will($this->returnValue(true));
171  $this->ilChatroomMock->expects($this->any())->method('getSetting')->will($this->returnValue('#_anonymous'));
172 
173  $first = $this->user->getChatNameSuggestions();
174  $second = $this->user->getChatNameSuggestions();
175 
176  $this->assertNotEquals($first, $second);
177  }
178 
180  {
181  $this->ilUserMock->expects($this->any())->method('isAnonymous')->will($this->returnValue(false));
182  $this->ilUserMock->expects($this->once())->method('getFirstname')->will($this->returnValue('John'));
183  $this->ilUserMock->expects($this->once())->method('getLastname')->will($this->returnValue('Doe'));
184  $this->ilUserMock->expects($this->once())->method('getPublicName')->will($this->returnValue('John Doe'));
185  $this->ilUserMock->expects($this->once())->method('getLogin')->will($this->returnValue('jdoe'));
186  $this->ilChatroomMock->expects($this->any())->method('getSetting')->will($this->returnValue('#_anonymous'));
187 
188  $suggestions = $this->user->getChatNameSuggestions();
189 
190  $this->assertEquals('John Doe', $suggestions['fullname']);
191  $this->assertEquals('J. Doe', $suggestions['shortname']);
192  $this->assertEquals('jdoe', $suggestions['login']);
193  }
194 
198  public function usernameDataProvider()
199  {
200  return array(
201  array('username', 'username'),
202  array('>username<', '&gt;username&lt;'),
203  );
204  }
205 }
$_SESSION["AccountId"]
user()
Definition: user.php:4
Class ilChatroomUserTest.
testSetUsername($username, $expected)
usernameDataProvider
Create styles array
The data for the language used.
Class ilChatroomUser.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27