14515310dSAndreas Gohr<?php 24515310dSAndreas Gohrnamespace dokuwiki\plugin\dbquery\test; 34515310dSAndreas Gohr 44515310dSAndreas Gohruse DokuWikiTest; 54515310dSAndreas Gohr 64515310dSAndreas Gohr/** 74515310dSAndreas Gohr * General tests for the dbquery plugin 84515310dSAndreas Gohr * 94515310dSAndreas Gohr * @group plugin_dbquery 104515310dSAndreas Gohr * @group plugins 114515310dSAndreas Gohr */ 124515310dSAndreas Gohrclass HelperTest extends DokuWikiTest 134515310dSAndreas Gohr{ 144515310dSAndreas Gohr protected $pluginsEnabled = array('dbquery'); 154515310dSAndreas Gohr /** @var \helper_plugin_dbquery $hlp */ 164515310dSAndreas Gohr protected $hlp; 174515310dSAndreas Gohr 184515310dSAndreas Gohr /** @inheritDoc */ 194515310dSAndreas Gohr public function setUp(): void 204515310dSAndreas Gohr { 214515310dSAndreas Gohr parent::setUp(); 224515310dSAndreas Gohr $this->hlp = plugin_load('helper', 'dbquery'); 234515310dSAndreas Gohr } 244515310dSAndreas Gohr 254515310dSAndreas Gohr public function testGatherVariables() 264515310dSAndreas Gohr { 274515310dSAndreas Gohr global $INFO; 284515310dSAndreas Gohr $INFO['id'] = 'foo:bar:baz'; 294515310dSAndreas Gohr 304515310dSAndreas Gohr $expected = [ 314515310dSAndreas Gohr ':user' => '', 324515310dSAndreas Gohr ':mail' => '', 334515310dSAndreas Gohr ':groups' => [], 344515310dSAndreas Gohr ':id' => ':foo:bar:baz', 354515310dSAndreas Gohr ':page' => 'baz', 364515310dSAndreas Gohr ':ns' => ':foo:bar', 374515310dSAndreas Gohr ]; 384515310dSAndreas Gohr $actual = $this->hlp->gatherVariables(); 394515310dSAndreas Gohr $this->assertEquals($expected, $actual); 404515310dSAndreas Gohr } 414515310dSAndreas Gohr 424515310dSAndreas Gohr public function testGatherVariablesUser() 434515310dSAndreas Gohr { 444515310dSAndreas Gohr global $INFO; 454515310dSAndreas Gohr /** @var \auth_plugin_authplain $auth */ 464515310dSAndreas Gohr global $auth; 474515310dSAndreas Gohr 484515310dSAndreas Gohr $INFO['id'] = 'foo:bar:baz'; 494515310dSAndreas Gohr $auth->createUser('foo', 'bar', 'My Test User', 'foo@example.com', ['one', 'two', 'three']); 504515310dSAndreas Gohr auth_login('foo', 'bar'); 514515310dSAndreas Gohr 524515310dSAndreas Gohr $expected = [ 534515310dSAndreas Gohr ':user' => 'foo', 544515310dSAndreas Gohr ':mail' => 'foo@example.com', 554515310dSAndreas Gohr ':groups' => ['one', 'two', 'three'], 564515310dSAndreas Gohr ':id' => ':foo:bar:baz', 574515310dSAndreas Gohr ':page' => 'baz', 584515310dSAndreas Gohr ':ns' => ':foo:bar', 594515310dSAndreas Gohr ]; 604515310dSAndreas Gohr $actual = $this->hlp->gatherVariables(); 614515310dSAndreas Gohr $this->assertEquals($expected, $actual); 624515310dSAndreas Gohr } 634515310dSAndreas Gohr 644515310dSAndreas Gohr public function testPrepareStatement() 654515310dSAndreas Gohr { 664515310dSAndreas Gohr $pdo = $this->hlp->getPDO('sqlite:', '', ''); 674515310dSAndreas Gohr 684515310dSAndreas Gohr $parameters = [ 694515310dSAndreas Gohr ':user' => 'foo', 704515310dSAndreas Gohr ':mail' => 'foo@example.com', 714515310dSAndreas Gohr ':groups' => ['one', 'two', 'three'], 724515310dSAndreas Gohr ':id' => ':foo:bar:baz', 734515310dSAndreas Gohr ':page' => 'baz', 744515310dSAndreas Gohr ':ns' => ':foo:bar', 754515310dSAndreas Gohr ]; 764515310dSAndreas Gohr $sql = 'SELECT :user, :mail, :id, :page, :ns WHERE \'foo\' in (:groups)'; 774515310dSAndreas Gohr $sth = $this->hlp->prepareStatement($pdo, $sql, $parameters); 784515310dSAndreas Gohr 794515310dSAndreas Gohr $actual = $sth->queryString; 804515310dSAndreas Gohr $expected = 'SELECT :user, :mail, :id, :page, :ns WHERE \'foo\' in (:group0,:group1,:group2)'; 814515310dSAndreas Gohr $this->assertEquals($expected, $actual); 824515310dSAndreas Gohr } 83*3113520cSAndreas Gohr 84*3113520cSAndreas Gohr public function testGetDsnAliases() 85*3113520cSAndreas Gohr { 86*3113520cSAndreas Gohr $conf = "nouser mysql:host=localhost;port=3306;dbname=testdb1\n\n". 87*3113520cSAndreas Gohr "nopass mysql:host=localhost;port=3306;dbname=testdb2 user\n". 88*3113520cSAndreas Gohr "both mysql:host=localhost;port=3306;dbname=testdb3 user pass\n"; 89*3113520cSAndreas Gohr 90*3113520cSAndreas Gohr $expect = [ 91*3113520cSAndreas Gohr '_' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb1', 'user' => 'dfu', 'pass' => 'dfp'], 92*3113520cSAndreas Gohr 'nouser' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb1', 'user' => 'dfu', 'pass' => 'dfp'], 93*3113520cSAndreas Gohr 'nopass' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb2', 'user' => 'user', 'pass' => 'dfp'], 94*3113520cSAndreas Gohr 'both' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb3', 'user' => 'user', 'pass' => 'pass'], 95*3113520cSAndreas Gohr ]; 96*3113520cSAndreas Gohr 97*3113520cSAndreas Gohr $actual = $this->callInaccessibleMethod($this->hlp, 'getDsnAliases', [$conf, 'dfu', 'dfp']); 98*3113520cSAndreas Gohr $this->assertEquals($expect, $actual); 99*3113520cSAndreas Gohr } 100*3113520cSAndreas Gohr 101*3113520cSAndreas Gohr public function testGetDsnAliasesLegacy() 102*3113520cSAndreas Gohr { 103*3113520cSAndreas Gohr $conf = 'mysql:host=localhost;port=3306;dbname=testdb1'; 104*3113520cSAndreas Gohr 105*3113520cSAndreas Gohr $expect = [ 106*3113520cSAndreas Gohr '_' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb1', 'user' => 'dfu', 'pass' => 'dfp'], 107*3113520cSAndreas Gohr ]; 108*3113520cSAndreas Gohr 109*3113520cSAndreas Gohr $actual = $this->callInaccessibleMethod($this->hlp, 'getDsnAliases', [$conf, 'dfu', 'dfp']); 110*3113520cSAndreas Gohr $this->assertEquals($expect, $actual); 111*3113520cSAndreas Gohr } 1124515310dSAndreas Gohr} 113