1<?php 2 3 4namespace dokuwiki\plugin\acknowledge\test; 5 6use DokuWikiTest; 7 8/** 9 * Helper tests for the acknowledge plugin 10 * 11 * @group plugin_acknowledge 12 * @group plugins 13 */ 14class HelperTest extends DokuWikiTest 15{ 16 /** @var array */ 17 protected $pluginsEnabled = ['acknowledge', 'sqlite']; 18 /** @var \helper_plugin_acknowledge $helper */ 19 protected $helper; 20 /** @var \helper_plugin_sqlite */ 21 protected $db; 22 23 public static function setUpBeforeClass(): void 24 { 25 parent::setUpBeforeClass(); 26 /** @var \auth_plugin_authplain $auth */ 27 global $auth; 28 $auth->createUser('max', 'none', 'max', 'max@example.com', ['super']); 29 } 30 31 public function setUp(): void 32 { 33 parent::setUp(); 34 $this->helper = plugin_load('helper', 'acknowledge'); 35 36 $this->db = $this->helper->getDB(); 37 38 $pages = "REPLACE INTO pages(page,lastmod) 39 VALUES ('dokuwiki:acktest1', 1560805365), 40 ('dokuwiki:acktest2', 1560805365), 41 ('dokuwiki:acktest3', 1560805365)"; 42 $this->db->query($pages); 43 44 $assignments = "REPLACE INTO assignments(page,pageassignees) 45 VALUES ('dokuwiki:acktest1', 'regular, @super'), 46 ('dokuwiki:acktest2', '@super'), 47 ('dokuwiki:acktest3', '@user')"; 48 $this->db->query($assignments); 49 50 $acks = "REPLACE INTO acks(page,user,ack) 51 VALUES ('dokuwiki:acktest3', 'regular', 1550801270), 52 ('dokuwiki:acktest3', 'regular', 1560805555), 53 ('dokuwiki:acktest1', 'max', 1550805770), 54 ('dokuwiki:acktest1', 'max', 1560805770), 55 ('dokuwiki:acktest3', 'max', 1560805000) 56 "; 57 $this->db->query($acks); 58 } 59 60 /** 61 * test latest acknowledgements 62 */ 63 public function test_getLatestAcknowledgements() 64 { 65 $actual = $this->helper->getAcknowledgements(); 66 $expected = [ 67 [ 68 'page' => 'dokuwiki:acktest1', 69 'user' => 'max', 70 'ack' => '1560805770', 71 'lastmod' => '1560805365', 72 ], 73 [ 74 'page' => 'dokuwiki:acktest3', 75 'user' => 'regular', 76 'ack' => '1560805555', 77 'lastmod' => '1560805365', 78 ], 79 [ 80 'page' => 'dokuwiki:acktest3', 81 'user' => 'max', 82 'ack' => '1560805000', 83 'lastmod' => '1560805365', 84 ], 85 ]; 86 $this->assertEquals($expected, $actual); 87 } 88 89 /** 90 * test latest acknowledgements limited to 1 91 */ 92 public function test_getLimitedAcknowledgements() 93 { 94 $actual = $this->helper->getAcknowledgements(1); 95 $expected = [ 96 [ 97 'page' => 'dokuwiki:acktest1', 98 'user' => 'max', 99 'ack' => '1560805770', 100 'lastmod' => '1560805365', 101 ], 102 ]; 103 $this->assertEquals($expected, $actual); 104 } 105 106 /** 107 * test assignment query 108 */ 109 public function test_getUserAssignments() 110 { 111 $actual = $this->helper->getUserAssignments('regular', ['user']); 112 $expected = [ 113 [ 114 'page' => 'dokuwiki:acktest1', 115 'pageassignees' => 'regular, @super', 116 'autoassignees' => '', 117 'lastmod' => '1560805365', 118 'user' => null, 119 'ack' => null, 120 ], 121 ]; 122 $this->assertEquals($expected, $actual); 123 124 $actual = $this->helper->getUserAssignments('max', ['user', 'super']); 125 $expected = [ 126 [ 127 'page' => 'dokuwiki:acktest2', 128 'pageassignees' => '@super', 129 'autoassignees' => '', 130 'lastmod' => '1560805365', 131 'user' => null, 132 'ack' => null, 133 ], 134 [ 135 'page' => 'dokuwiki:acktest3', 136 'pageassignees' => '@user', 137 'autoassignees' => '', 138 'lastmod' => '1560805365', 139 'user' => null, 140 'ack' => null, 141 ], 142 ]; 143 $this->assertEquals($expected, $actual); 144 } 145 146 public function test_getUserAcknowledgements() 147 { 148 $actual = $this->helper->getUserAcknowledgements('max', ['user', 'super']); 149 $expected = [ 150 [ 151 'page' => 'dokuwiki:acktest1', 152 'pageassignees' => 'regular, @super', 153 'autoassignees' => '', 154 'lastmod' => '1560805365', 155 'user' => 'max', 156 'ack' => '1560805770', 157 ], 158 [ 159 'page' => 'dokuwiki:acktest2', 160 'pageassignees' => '@super', 161 'autoassignees' => '', 162 'lastmod' => '1560805365', 163 'user' => null, 164 'ack' => null, 165 ], 166 [ 167 'page' => 'dokuwiki:acktest3', 168 'pageassignees' => '@user', 169 'autoassignees' => '', 170 'lastmod' => '1560805365', 171 'user' => 'max', 172 'ack' => '1560805000', 173 ], 174 ]; 175 $this->assertEquals($expected, $actual); 176 } 177 178 /** 179 * Check what users are assigned to a page that has a user and a group in the database 180 */ 181 public function test_getPageAssignees() 182 { 183 $actual = $this->helper->getPageAssignees('dokuwiki:acktest1'); 184 $expected = ['regular', 'max']; 185 $this->assertEquals($expected, $actual); 186 } 187 188 /** 189 * Check what acknowledgments are there for a page 190 */ 191 public function test_getPageAcknowledgements() 192 { 193 $actual = $this->helper->getPageAcknowledgements('dokuwiki:acktest1'); 194 $expected = [ 195 [ 196 'page' => 'dokuwiki:acktest1', 197 'lastmod' => '1560805365', 198 'user' => 'max', 199 'ack' => '1560805770', 200 ], 201 [ 202 'page' => 'dokuwiki:acktest1', 203 'lastmod' => '1560805365', 204 'user' => 'regular', 205 'ack' => null, 206 ], 207 208 ]; 209 $this->assertEquals($expected, $actual); 210 211 } 212} 213