Skip to content

Commit 5f51eb5

Browse files
authored
Merge pull request #94 from funktechno/f/tests
F/tests
2 parents 5a0ef00 + f3f37be commit 5f51eb5

File tree

1 file changed

+285
-21
lines changed

1 file changed

+285
-21
lines changed

Test/Model/WikiModelTest.php

+285-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<?php
22

3+
34
require_once 'tests/units/Base.php';
45

56
use Kanboard\Core\Plugin\Loader;
67
use Kanboard\Plugin\Wiki\Model\WikiModel;
78
use Kanboard\Model\ProjectModel;
9+
use Kanboard\Core\Controller\PageNotFoundException;
10+
use Kanboard\Core\Controller\AccessForbiddenException;
811
use Kanboard\Core\User\UserSession;
912
use Kanboard\Core\Security\AuthenticationManager;
1013
use Kanboard\Auth\DatabaseAuth;
@@ -15,11 +18,12 @@ class WikiModelTest extends Base
1518
* @var Plugin
1619
*/
1720
protected $plugin;
21+
private $wikiModel;
22+
private $db;
1823

1924
protected function setUp(): void
2025
{
2126
parent::setUp();
22-
// $this->plugin = new Plugin($this->container);
2327

2428
$plugin = new Loader($this->container);
2529
$plugin->scan();
@@ -28,6 +32,11 @@ protected function setUp(): void
2832
$authManager->register(new DatabaseAuth($this->container));
2933

3034
$_SESSION['user'] = array('id' => 1, 'username' => 'test', 'role' => 'app-admin');
35+
36+
// $this->db = $this->createMock(Base::class);
37+
$this->wikiModel = new WikiModel($this->container);
38+
39+
// $this->wikiModel = new WikiModel($this->container);
3140
}
3241

3342
public function testCreation()
@@ -38,16 +47,16 @@ public function testCreation()
3847

3948
$project = $projectModel->getById(1);
4049

41-
$wikimodel = new WikiModel($this->container);
50+
// $this->wikiModel = new WikiModel($this->container);
4251
// create wiki pages
4352

4453
$titleValue = "Security";
4554
$contentValue = "Some content";
46-
$this->assertEquals(1, $wikimodel->createpage($project['id'], $titleValue, $contentValue, '2015-01-01'), 'Failed to a create wiki page on project');
47-
$this->assertEquals(2, $wikimodel->createpage($project['id'], "Conventions", 'More content'), 'Failed to an additional create wiki page on project');
55+
$this->assertEquals(1, $this->wikiModel->createpage($project['id'], $titleValue, $contentValue, '2015-01-01'), 'Failed to a create wiki page on project');
56+
$this->assertEquals(2, $this->wikiModel->createpage($project['id'], "Conventions", 'More content'), 'Failed to an additional create wiki page on project');
4857

4958
// grab editions for first wiki page
50-
$editions = $wikimodel->getEditions(1);
59+
$editions = $this->wikiModel->getEditions(1);
5160
$this->assertEmpty($editions);
5261

5362
$values = [
@@ -60,10 +69,10 @@ public function testCreation()
6069

6170
$this->userSession = new UserSession($this->container);
6271
// result is not a consistent 1. is this true or id for new edition?
63-
$createEditionResult = $wikimodel->createEdition($values, 1, 1);
64-
// $this->assertEquals($wikimodel->createEdition($values, 1, 1), 1, 'Failed to create wiki edition');
72+
$createEditionResult = $this->wikiModel->createEdition($values, 1, 1);
73+
// $this->assertEquals($this->wikiModel->createEdition($values, 1, 1), 1, 'Failed to create wiki edition');
6574

66-
$editions = $wikimodel->getEditions(1);
75+
$editions = $this->wikiModel->getEditions(1);
6776
$this->assertNotEmpty($editions, 'Failed to get wiki editions');
6877

6978
$this->assertEquals('Security', $editions[0]['title']);
@@ -78,20 +87,20 @@ public function testReOrder(){
7887

7988
$project = $projectModel->getById(1);
8089

81-
$wikimodel = new WikiModel($this->container);
90+
// $this->wikiModel = new WikiModel($this->container);
8291

8392
// create wiki pages
84-
$this->assertEquals(1, $wikimodel->createpage($project['id'], "Home", "", '2015-01-01'), 1, 'Failed to a create wiki page home on project');
93+
$this->assertEquals(1, $this->wikiModel->createpage($project['id'], "Home", "", '2015-01-01'), 1, 'Failed to a create wiki page home on project');
8594
for ($i=2; $i <= 5; $i++) {
86-
$this->assertEquals($i, $wikimodel->createpage($project['id'], "Page ". $i, ""), 'Failed to a create wiki page '. $i . ' on project');
95+
$this->assertEquals($i, $this->wikiModel->createpage($project['id'], "Page ". $i, ""), 'Failed to a create wiki page '. $i . ' on project');
8796
}
8897

8998
// reorder
90-
$wikimodel->reorderPages($project['id'], 5, 3);
99+
$this->wikiModel->reorderPages($project['id'], 5, 3);
91100
// expected by id
92101
$expectedColumnOrders = [1,2,4,5,3];
93102

94-
$wikiPages = $wikimodel->getWikipages($project['id']);
103+
$wikiPages = $this->wikiModel->getWikipages($project['id']);
95104
$this->assertEquals(count($expectedColumnOrders), count($wikiPages), 'expected column order count doesn\'t match pages');
96105

97106
for ($i=0; $i < count($expectedColumnOrders); $i++) {
@@ -107,34 +116,289 @@ public function testReOrderByIndex(){
107116

108117
$project = $projectModel->getById(1);
109118

110-
$wikimodel = new WikiModel($this->container);
119+
// $this->wikiModel = new WikiModel($this->container);
111120

112121
// create wiki pages
113-
$this->assertEquals(1, $wikimodel->createpage($project['id'], "Home", "", '2015-01-01'), 1, 'Failed to a create wiki page home on project');
122+
$this->assertEquals(1, $this->wikiModel->createpage($project['id'], "Home", "", '2015-01-01'), 1, 'Failed to a create wiki page home on project');
114123
for ($i=2; $i <= 5; $i++) {
115-
$this->assertEquals($i, $wikimodel->createpage($project['id'], "Page ". $i, ""), 'Failed to a create wiki page '. $i . ' on project');
124+
$this->assertEquals($i, $this->wikiModel->createpage($project['id'], "Page ". $i, ""), 'Failed to a create wiki page '. $i . ' on project');
116125
}
117126

118127
// make page 5 a child of home page
119-
$wikimodel->updatepage(array('id' => 5, 'parent_id' => 1, 'title' => 'Page 5', 'editions' => 1, 'content' => 'Some content'), 1, '2015-01-01');
128+
$this->wikiModel->updatepage(array('id' => 5, 'parent_id' => 1, 'title' => 'Page 5', 'editions' => 1, 'content' => 'Some content'), 1, '2015-01-01');
120129

121-
$childPage = $wikimodel->getWikipage(5);
130+
$childPage = $this->wikiModel->getWikipage(5);
122131

123132
$this->assertEquals(5, $childPage['id']);
124133
$this->assertEquals(1, $childPage['parent_id']);
125134
$this->assertEquals('Page 5', $childPage['title']);
126135
$this->assertEquals('Some content', $childPage['content']);
127136

128137
// reorder
129-
$wikimodel->reorderPagesByIndex($project['id'], 4, 2, null);
138+
$this->wikiModel->reorderPagesByIndex($project['id'], 4, 2, null);
130139
// expected by id
131140
$expectedColumnOrders = [1,3,4,2];
132141

133-
$wikiPages = $wikimodel->getWikiPagesByParentId($project['id'], null);
142+
$wikiPages = $this->wikiModel->getWikiPagesByParentId($project['id'], null);
134143
$this->assertEquals(count($expectedColumnOrders), count($wikiPages), 'expected column order count doesn\'t match pages');
135144

136145
for ($i=0; $i < count($expectedColumnOrders); $i++) {
137146
$this->assertEquals($expectedColumnOrders[$wikiPages[$i]['id']-1], $wikiPages[$i]['ordercolumn'], 'Failed to reorder page id:'. $wikiPages[$i]['id']);
138147
}
139148
}
140-
}
149+
/*
150+
public function testGetEditionsReturnsArray()
151+
{
152+
$wikiId = 1;
153+
$expectedResult = [
154+
['id' => 1, 'title' => 'My page', 'content' => 'Content of the page', 'edition' => 1]
155+
];
156+
157+
// $this->db
158+
// ->expects($this->once())
159+
// ->method('table')
160+
// ->with(WikiModel::WIKI_EDITION_TABLE)
161+
// ->willReturn($this->db);
162+
163+
// $this->db
164+
// ->expects($this->once())
165+
// ->method('eq')
166+
// ->with('wikipage_id', $wikiId)
167+
// ->willReturn($this->db);
168+
169+
// $this->db
170+
// ->expects($this->once())
171+
// ->method('desc')
172+
// ->with('edition')
173+
// ->willReturn($this->db);
174+
175+
// $this->db
176+
// ->expects($this->once())
177+
// ->method('findAll')
178+
// ->willReturn($expectedResult);
179+
180+
$result = $this->wikiModel->getEditions($wikiId);
181+
$this->assertEquals(json_encode($expectedResult), json_encode($result));
182+
}
183+
*/
184+
public function testCreatePageSavesCorrectly()
185+
{
186+
// $this->db
187+
// ->expects($this->once())
188+
// ->method('table')
189+
// ->with(WikiModel::WIKITABLE)
190+
// ->willReturnSelf();
191+
192+
// $this->db
193+
// ->method('persist')
194+
// ->willReturn(1);
195+
196+
$result = $this->wikiModel->createpage(1, 'Test title', 'Test content');
197+
198+
$this->assertEquals(false, $result);
199+
}
200+
/*
201+
public function testUpdatePageUpdatesCorrectly()
202+
{
203+
// $this->db
204+
// ->expects($this->once())
205+
// ->method('table')
206+
// ->with(WikiModel::WIKITABLE)
207+
// ->willReturnSelf();
208+
209+
// $this->db
210+
// ->method('eq')
211+
// ->with('id', 1)
212+
// ->willReturnSelf();
213+
214+
// $this->db
215+
// ->method('update')
216+
// ->willReturn(true);
217+
218+
// $result = $this->wikiModel->updatepage(['title' => 'New Title', 'content' => 'New content', 'id' => 1], 1);
219+
220+
// $this->assertEquals(1, $result);
221+
}
222+
223+
public function testRemovePageRemovesCorrectly()
224+
{
225+
// $this->db
226+
// ->expects($this->once())
227+
// ->method('table')
228+
// ->with(WikiModel::WIKITABLE)
229+
// ->willReturnSelf();
230+
231+
// $this->db
232+
// ->method('eq')
233+
// ->with('id', 1)
234+
// ->willReturnSelf();
235+
236+
// $this->db
237+
// ->method('remove')
238+
// ->willReturn(true);
239+
/*
240+
$result = $this->wikiModel->removepage(1);
241+
242+
$this->assertTrue($result);
243+
244+
}
245+
246+
public function testGetWikipageReturnsCorrectData()
247+
{
248+
// $this->db
249+
// ->expects($this->once())
250+
// ->method('table')
251+
// ->with(WikiModel::WIKITABLE)
252+
// ->willReturnSelf();
253+
254+
// $this->db
255+
// ->method('columns')
256+
// ->willReturnSelf();
257+
258+
// $this->db
259+
// ->method('eq')
260+
// ->with('id', 1)
261+
// ->willReturnSelf();
262+
263+
$expected = ['id' => 1, 'title' => 'Test'];
264+
265+
// $this->db
266+
// ->method('findOne')
267+
// ->willReturn($expected);
268+
/*
269+
$result = $this->wikiModel->getWikipage(1);
270+
271+
$this->assertSame($expected, $result);
272+
273+
}
274+
275+
public function testGetLatestEditionReturnsArray()
276+
{
277+
$wikiId = 1;
278+
$expectedResult = ['id' => 1, 'title' => 'Page Title', 'edition' => 2];
279+
280+
// $this->db
281+
// ->expects($this->once())
282+
// ->method('table')
283+
// ->with(WikiModel::WIKI_EDITION_TABLE)
284+
// ->willReturn($this->db);
285+
286+
// $this->db
287+
// ->expects($this->once())
288+
// ->method('eq')
289+
// ->with('wikipage_id', $wikiId)
290+
// ->willReturn($this->db);
291+
292+
// $this->db
293+
// ->expects($this->once())
294+
// ->method('desc')
295+
// ->with('edition')
296+
// ->willReturn($this->db);
297+
298+
// $this->db
299+
// ->expects($this->once())
300+
// ->method('findOne')
301+
// ->willReturn($expectedResult);
302+
/*
303+
$result = $this->wikiModel->getLatestEdition($wikiId);
304+
$this->assertEquals($expectedResult, $result);
305+
306+
}
307+
308+
public function testRemovePageWithNonExistentIdReturnsFalse()
309+
{
310+
$wikiId = 999;
311+
312+
// $this->db
313+
// ->expects($this->once())
314+
// ->method('table')
315+
// ->with(WikiModel::WIKITABLE)
316+
// ->willReturn($this->db);
317+
318+
// $this->db
319+
// ->expects($this->once())
320+
// ->method('eq')
321+
// ->with('id', $wikiId)
322+
// ->willReturn($this->db);
323+
324+
// $this->db
325+
// ->expects($this->once())
326+
// ->method('remove')
327+
// ->willReturn(false);
328+
/*
329+
$result = $this->wikiModel->removepage($wikiId);
330+
$this->assertFalse($result);
331+
332+
}
333+
334+
public function testGetWikipageThrowsPageNotFoundException()
335+
{
336+
// $this->db
337+
// ->method('findOne')
338+
// ->willReturn(null);
339+
/*
340+
$this->expectException(\Kanboard\Core\ExternalTask\NotFoundException::class);
341+
342+
$this->wikiModel->getWiki();
343+
344+
}
345+
346+
public function testGetWikiPageThrowsPageNotFoundException2()
347+
{
348+
$this->expectException(PageNotFoundException::class);
349+
$wikiId = 999;
350+
351+
// $this->db
352+
// ->expects($this->once())
353+
// ->method('table')
354+
// ->with(WikiModel::WIKITABLE)
355+
// ->willReturn($this->db);
356+
357+
// $this->db
358+
// ->expects($this->once())
359+
// ->method('eq')
360+
// ->with('id', $wikiId)
361+
// ->willReturn($this->db);
362+
363+
// $this->db
364+
// ->expects($this->once())
365+
// ->method('findOne')
366+
// ->willReturn(null);
367+
/*
368+
$this->wikiModel->getWikipage($wikiId);
369+
370+
}
371+
372+
public function testCreatePageWithValidData()
373+
{
374+
$project_id = 1;
375+
$title = 'New Page';
376+
$content = 'Content';
377+
$date = date('Y-m-d');
378+
$values = [
379+
'project_id' => $project_id,
380+
'title' => $title,
381+
'content' => $content,
382+
'date_creation' => $date,
383+
'ordercolumn' => 1
384+
];
385+
386+
// $this->db
387+
// ->expects($this->once())
388+
// ->method('table')
389+
// ->with(WikiModel::WIKITABLE)
390+
// ->willReturn($this->db);
391+
392+
// $this->db
393+
// ->expects($this->once())
394+
// ->method('persist')
395+
// ->with($values);
396+
/*
397+
$result = $this->wikiModel->createpage($project_id, $title, $content, $date);
398+
$this->assertNotNull($result);
399+
400+
}
401+
*/
402+
403+
// Additional test cases can be defined here for each method
404+
}

0 commit comments

Comments
 (0)