[icinga-checkins] icinga.org: icingaweb2/master: FilterExpression: render boolean true as such

git at icinga.org git at icinga.org
Wed Aug 3 15:07:34 CEST 2016


Module: icingaweb2
Branch: master
Commit: 2b50f6ff1073e9ad4a47ee291cc3a5bca8f6e58a
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=2b50f6ff1073e9ad4a47ee291cc3a5bca8f6e58a

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Aug  3 13:03:08 2016 +0000

FilterExpression: render boolean true as such

fixes #12299

---

 library/Icinga/Data/Filter/FilterExpression.php    |   13 +++++++++++++
 test/php/library/Icinga/Data/Filter/FilterTest.php |   14 ++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/library/Icinga/Data/Filter/FilterExpression.php b/library/Icinga/Data/Filter/FilterExpression.php
index ad6105c..87cd733 100644
--- a/library/Icinga/Data/Filter/FilterExpression.php
+++ b/library/Icinga/Data/Filter/FilterExpression.php
@@ -107,6 +107,10 @@ class FilterExpression extends Filter
 
     public function __toString()
     {
+        if ($this->isBooleanTrue()) {
+            return $this->column;
+        }
+
         $expression = is_array($this->expression) ?
              '( ' . implode(' | ', $this->expression) . ' )' :
              $this->expression;
@@ -121,6 +125,10 @@ class FilterExpression extends Filter
 
     public function toQueryString()
     {
+        if ($this->isBooleanTrue()) {
+            return $this->column;
+        }
+
         $expression = is_array($this->expression) ?
              '(' . implode('|', array_map('rawurlencode', $this->expression)) . ')' :
              rawurlencode($this->expression);
@@ -128,6 +136,11 @@ class FilterExpression extends Filter
         return $this->column . $this->sign . $expression;
     }
 
+    protected function isBooleanTrue()
+    {
+        return $this->sign === '=' && $this->expression === true;
+    }
+
     /**
      * If $var is a scalar, do the same as strtolower() would do.
      * If $var is an array, map $this->strtolowerRecursive() to its elements.
diff --git a/test/php/library/Icinga/Data/Filter/FilterTest.php b/test/php/library/Icinga/Data/Filter/FilterTest.php
index 8b93360..ef64b5a 100644
--- a/test/php/library/Icinga/Data/Filter/FilterTest.php
+++ b/test/php/library/Icinga/Data/Filter/FilterTest.php
@@ -198,6 +198,20 @@ class FilterTest extends BaseTestCase
         $this->assertNotEquals((string) $c, (string) $d);
     }
 
+    public function testBooleanExpressionIsRenderedCorrectly()
+    {
+        $filter = Filter::fromQueryString('a&!b');
+        $this->assertEquals(
+            $filter->toQueryString(),
+            'a&!b'
+        );
+        $this->assertEquals(
+            (string) $filter,
+            // TODO: I'd prefer to see 'a & !b' here:
+            'a & (! b)'
+        );
+    }
+
     public function testLeadingAndTrailingWhitespaces()
     {
         $columnWithWhitespaces = Filter::where(' host ', 'localhost');



More information about the icinga-checkins mailing list