From 191b063a063ad5a2198bbdc0bc540ebe24ed693b Mon Sep 17 00:00:00 2001 From: Carsten Rose <carsten.rose@math.uzh.ch> Date: Sat, 14 May 2016 14:59:36 +0200 Subject: [PATCH] Report.php: cleanup of Report class. Workaround to suppress 'undefined index'. PHP Unit Tests: LinktTest.php, ReportTest.phh - more tests --- extension/qfq/qfq/report/Report.php | 111 +++++++++++++-------- extension/qfq/tests/phpunit/LinkTest.php | 20 +++- extension/qfq/tests/phpunit/ReportTest.php | 24 +++++ 3 files changed, 108 insertions(+), 47 deletions(-) diff --git a/extension/qfq/qfq/report/Report.php b/extension/qfq/qfq/report/Report.php index 021ae8f8b..6fb1698b5 100644 --- a/extension/qfq/qfq/report/Report.php +++ b/extension/qfq/qfq/report/Report.php @@ -70,6 +70,7 @@ class Report { // private $resultArray = array(); private $levelCount = 0; //private $counter = 0; + /** * @var Variables */ @@ -90,9 +91,14 @@ class Report { */ private $sendmail = null; - private $page_control = array(); + /** + * @var array + */ + private $pageDefaults = array(); - // Emulate global variable: will be set much earlier in other functions. Will be shown in error messages. + /** + * @var array - Emulate global variable: will be set much earlier in other functions. Will be shown in error messages. + */ private $fr_error = array('uid' => '', 'pid' => '', 'row' => '', 'debug_level' => '0', 'full_level' => ''); private $phpUnit = false; @@ -120,18 +126,18 @@ class Report { $this->showDebugInfo = ($this->store->getVar(SYSTEM_SHOW_DEBUG_INFO, STORE_SYSTEM) === 'yes'); - $this->page_control["msgbox"]["pagec"] = "Please confirm!"; + $this->pageDefaults["msgbox"]["pagec"] = "Please confirm!"; - $this->page_control["sip"]["paged"] = TOKEN_SIP; - $this->page_control["sip"]["pagee"] = TOKEN_SIP; - $this->page_control["sip"]["pagen"] = TOKEN_SIP; + $this->pageDefaults["sip"]["paged"] = TOKEN_SIP; + $this->pageDefaults["sip"]["pagee"] = TOKEN_SIP; + $this->pageDefaults["sip"]["pagen"] = TOKEN_SIP; - $this->page_control["icon"]["paged"] = TOKEN_DELETE; - $this->page_control["icon"]["pagee"] = TOKEN_EDIT; - $this->page_control["icon"]["pageh"] = TOKEN_HELP; - $this->page_control["icon"]["pagei"] = TOKEN_INFO; - $this->page_control["icon"]["pagen"] = TOKEN_NEW; - $this->page_control["icon"]["pages"] = TOKEN_SHOW; + $this->pageDefaults["icon"]["paged"] = TOKEN_DELETE; + $this->pageDefaults["icon"]["pagee"] = TOKEN_EDIT; + $this->pageDefaults["icon"]["pageh"] = TOKEN_HELP; + $this->pageDefaults["icon"]["pagei"] = TOKEN_INFO; + $this->pageDefaults["icon"]["pagen"] = TOKEN_NEW; + $this->pageDefaults["icon"]["pages"] = TOKEN_SHOW; $this->db = new Database(); $this->utils = new Utils(); @@ -320,10 +326,15 @@ class Report { } if ($fnBody) { - error_reporting(error_reporting() & ~E_NOTICE); $sortFn = create_function('$a,$b', $fnBody); + + // TODO: at the moment, $sortFn() triggers some E_NOTICE warnings. We stop these here for a short time. + $errorSet = error_reporting(); + error_reporting($errorSet & ~E_NOTICE); + usort($ary, $sortFn); - error_reporting(error_reporting() | E_NOTICE); + + error_reporting($errorSet); } } @@ -784,52 +795,65 @@ class Report { * * $columnValue: * ------------- - * [<page id|alias>[¶m=value&...]] | [record id] | [text] | [tooltip] | [msgbox] | [class] | [target] | [render mode] | [create hash] + * [<page id|alias>[¶m=value&...]] | [text] | [tooltip] | [msgbox] | [class] | [target] | [render mode] * * param[0]: <page id|alias>[¶m=value&...] - * param[1]: record id * param[2]: text * param[3]: tooltip * param[4]: msgbox * param[5]: class * param[6]: target * param[7]: render mode - * param[8]: create hash * @throws SyntaxReportException */ private function doFixColPosPage($columnName, $columnValue) { $link = ""; + if (empty($columnName)) + return ''; + // Split definition - $param = explode('|', $columnValue); - if (count($param) > 8) + $allParam = explode('|', $columnValue); + if (count($allParam) > 8) throw new SyntaxReportException ("Too many parameter (max=8): $columnValue", "", null, __FILE__, __LINE__, $this->fr_error); - // make first 'P' lowercase - $columnName[0] = 'p'; + $columnName[0] = 'p'; // make first 'P' lowercase + + // First Parameter: Split PageId|PageAlias and URL Params + $firstParam = explode('&', $allParam[0], 2); + if (empty($firstParam[1])) { + $firstParam[] = ''; + } + + $link .= $this->composeLinkPart(TOKEN_PAGE, $firstParam[0]); // -- PageID -- + $link .= $this->composeLinkPart(TOKEN_URL_PARAM, $firstParam[1]); + + if (!empty($allParam[1])) + $link .= $this->composeLinkPart(TOKEN_TEXT, $allParam[1]); // -- Text -- + + if (!empty($allParam[2])) + $link .= $this->composeLinkPart(TOKEN_TOOL_TIP, $allParam[2]); // -- tooltip -- + + if (!empty($allParam[3])) + $link .= $this->composeLinkPart(TOKEN_QUESTION, $allParam[3], $this->pageDefaults["msgbox"][$columnName]); // -- msgbox + + if (!empty($allParam[4])) + $link .= $this->composeLinkPart(TOKEN_CLASS, $allParam[4]); // -- class -- - // -- Page -- - // Split PageId|PageAlias and URL Params - $tmparr = explode('&', $param[0], 2); + if (!empty($allParam[5])) + $link .= $this->composeLinkPart(TOKEN_TARGET, $allParam[5]); // -- target -- - $link .= $this->composeLinkPart(TOKEN_PAGE, $tmparr[0]); // -- PageID -- - $link .= $this->composeLinkPart(TOKEN_URL_PARAM, $tmparr[1]); - $link .= $this->composeLinkPart(TOKEN_TEXT, $param[1]); // -- Text -- - $link .= $this->composeLinkPart(TOKEN_TOOL_TIP, $param[2]); // -- tooltip -- - $link .= $this->composeLinkPart(TOKEN_QUESTION, $param[3], $this->page_control["msgbox"][$columnName]); // -- msgbox - $link .= $this->composeLinkPart(TOKEN_CLASS, $param[4]); // -- class -- - $link .= $this->composeLinkPart(TOKEN_TARGET, $param[5]); // -- target -- - $link .= $this->composeLinkPart(TOKEN_RENDER, $param[6]); // -- render mode -- + if (!empty($allParam[6])) + $link .= $this->composeLinkPart(TOKEN_RENDER, $allParam[6]); // -- render mode -- - if (!$param[7]) - $param[7] = $this->page_control["sip"][$columnName]; // if no hash behaviour defined, use default + if (!empty($allParam[7])) + $allParam[7] = $this->pageDefaults["sip"][$columnName]; // if no SIP behaviour defined, use default - if ($param[7] === TOKEN_SIP) - $link .= TOKEN_SIP . "|"; + $link .= TOKEN_SIP . "|"; // page always get a sip - if ($this->page_control["icon"][$columnName]) - $link .= $this->page_control["icon"][$columnName] . "|"; + if ($this->pageDefaults["icon"][$columnName]) + $link .= $this->pageDefaults["icon"][$columnName] . "|"; return ($link); } @@ -854,9 +878,10 @@ class Report { */ private function composeLinkPart($qualifier, $value, $defaultValue = "") { - if (!$value) $value = $defaultValue; + if (empty($value)) + $value = $defaultValue; - if ($value) + if (!empty($value)) return ($qualifier . ":" . $value . "|"); return ''; @@ -877,11 +902,11 @@ class Report { $param = explode('|', $columnValue); # get all defaultvalues, depending on the columnname - $defaultImage = $this->page_control["icon"][$columnName]; - $defaultHash = $this->page_control["sip"][$columnName]; + $defaultImage = $this->pageDefaults["icon"][$columnName]; + $defaultHash = $this->pageDefaults["sip"][$columnName]; # define defaultquestion only, if pagetype needs a question - if ($this->page_control["msgbox"][$columnName]) $defaultQuestion = 'q:' . $this->page_control["msgbox"][$columnName]; + if ($this->pageDefaults["msgbox"][$columnName]) $defaultQuestion = 'q:' . $this->pageDefaults["msgbox"][$columnName]; foreach ($param as $key) { switch (substr($key, 0, 1)) { diff --git a/extension/qfq/tests/phpunit/LinkTest.php b/extension/qfq/tests/phpunit/LinkTest.php index 1afa07637..b4e4ca349 100644 --- a/extension/qfq/tests/phpunit/LinkTest.php +++ b/extension/qfq/tests/phpunit/LinkTest.php @@ -43,8 +43,17 @@ class LinkTest extends \PHPUnit_Framework_TestCase { $result = $link->renderLink('u:http://example.com'); $this->assertEquals('<a href="http://example.com" class="external" >http://example.com</a>', $result); + $result = $link->renderLink('u:http://example.com?id=100&t=2¶m=hello'); + $this->assertEquals('<a href="http://example.com?id=100&t=2¶m=hello" class="external" >http://example.com?id=100&t=2¶m=hello</a>', $result); + + $result = $link->renderLink('u:example.com'); + $this->assertEquals('<a href="example.com" class="external" >example.com</a>', $result); + $result = $link->renderLink('u:http://example.com|t:Hello world'); $this->assertEquals('<a href="http://example.com" class="external" >Hello world</a>', $result); + + $result = $link->renderLink('u:http://example.com?id=100&t=2¶m=hello|t:Hello world'); + $this->assertEquals('<a href="http://example.com?id=100&t=2¶m=hello" class="external" >Hello world</a>', $result); } /** @@ -77,6 +86,9 @@ class LinkTest extends \PHPUnit_Framework_TestCase { $result = $link->renderLink('p:id=secondPage'); $this->assertEquals('<a href="?id=secondPage" class="internal" >?id=secondPage</a>', $result); + $result = $link->renderLink('p:id=secondPage&id=100&t=2¶m=hello'); + $this->assertEquals('<a href="?id=secondPage&id=100&t=2¶m=hello" class="internal" >?id=secondPage&id=100&t=2¶m=hello</a>', $result); + $result = $link->renderLink('p:secondPage|t:Hello world'); $this->assertEquals('<a href="?id=secondPage" class="internal" >Hello world</a>', $result); } @@ -888,12 +900,12 @@ class LinkTest extends \PHPUnit_Framework_TestCase { $link = new Link(null, $this->sip, true); // Sip: URL - $result = $link->renderLink('u:?form&r=12&xId=2345|s'); - $this->assertEquals('<a href="index.php?id=form&s=badcaffee1234" class="external" >index.php?id=form&s=badcaffee1234</a>', $result); + $result = $link->renderLink('u:?form&r=12&xId=2345&L=1&type=99&gId=55|s'); + $this->assertEquals('<a href="index.php?id=form&L=1&type=99&s=badcaffee1234" class="external" >index.php?id=form&L=1&type=99&s=badcaffee1234</a>', $result); // Sip: Page - $result = $link->renderLink('p:form&r=12&xId=2345|s'); - $this->assertEquals('<a href="index.php?id=form&s=badcaffee1234" class="internal" >index.php?id=form&s=badcaffee1234</a>', $result); + $result = $link->renderLink('p:form&r=12&xId=2345&L=1&type=99&gId=55|s'); + $this->assertEquals('<a href="index.php?id=form&L=1&type=99&s=badcaffee1234" class="internal" >index.php?id=form&L=1&type=99&s=badcaffee1234</a>', $result); } diff --git a/extension/qfq/tests/phpunit/ReportTest.php b/extension/qfq/tests/phpunit/ReportTest.php index b2e0f324f..7955c8ab8 100644 --- a/extension/qfq/tests/phpunit/ReportTest.php +++ b/extension/qfq/tests/phpunit/ReportTest.php @@ -182,6 +182,30 @@ class ReportTest extends AbstractDatabaseTest { } + /** + * + */ + public function testReportPageToken() { + + // +// $result = $this->report->process("10.sql = SELECT 'p:form&r=123&a=hello&type=5&L=3&final=world|t:Person' AS _page FROM Person ORDER BY id LIMIT 1"); +// $this->assertEquals('', $result); + + + } + + /** + * + */ + public function testReportPageFix() { + + // +// $result = $this->report->process("10.sql = SELECT 'form|r=123&a=hello&type=5&L=3&final=world|Person' AS _Page FROM Person ORDER BY id LIMIT 1"); +// $this->assertEquals('', $result); + + + } + /** * @throws Exception -- GitLab