summaryrefslogtreecommitdiffstats
path: root/test/php/library/Director/Data
diff options
context:
space:
mode:
Diffstat (limited to 'test/php/library/Director/Data')
-rw-r--r--test/php/library/Director/Data/AssignFilterHelperTest.php86
-rw-r--r--test/php/library/Director/Data/RecursiveUtf8ValidatorTest.php45
2 files changed, 131 insertions, 0 deletions
diff --git a/test/php/library/Director/Data/AssignFilterHelperTest.php b/test/php/library/Director/Data/AssignFilterHelperTest.php
new file mode 100644
index 0000000..5fcdd95
--- /dev/null
+++ b/test/php/library/Director/Data/AssignFilterHelperTest.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Tests\Icinga\Module\Director\IcingaConfig;
+
+use Icinga\Data\Filter\Filter;
+use Icinga\Module\Director\Data\AssignFilterHelper;
+use Icinga\Module\Director\Objects\HostApplyMatches;
+use Icinga\Module\Director\Test\BaseTestCase;
+
+class AssignFilterHelperTest extends BaseTestCase
+{
+ protected static $exampleHost;
+
+ public static function setUpBeforeClass()
+ {
+ self::$exampleHost = (object) [
+ 'address' => '127.0.0.1',
+ 'vars.operatingsystem' => 'centos',
+ 'vars.customer' => 'ACME',
+ 'vars.roles' => ['webserver', 'mailserver'],
+ 'vars.bool_string' => 'true',
+ 'groups' => ['web-server', 'mail-server'],
+ ];
+ }
+
+ public function testSimpleApplyFilter()
+ {
+ $this->assertFilterOutcome(true, 'host.address=true', self::$exampleHost);
+ $this->assertFilterOutcome(false, 'host.address=false', self::$exampleHost);
+ $this->assertFilterOutcome(true, 'host.address=false', (object) ['address' => null]);
+ $this->assertFilterOutcome(false, 'host.address=true', (object) ['address' => null]);
+ $this->assertFilterOutcome(true, 'host.address=%22127.0.0.%2A%22', self::$exampleHost);
+ }
+
+ public function testListApplyFilter()
+ {
+ $this->assertFilterOutcome(true, 'host.vars.roles=%22*server%22', self::$exampleHost);
+ $this->assertFilterOutcome(true, 'host.groups=%22*-server%22', self::$exampleHost);
+ $this->assertFilterOutcome(false, 'host.groups=%22*-nothing%22', self::$exampleHost);
+ }
+
+ public function testComplexApplyFilter()
+ {
+ $this->assertFilterOutcome(
+ true,
+ 'host.vars.operatingsystem=%5B%22centos%22%2C%22fedora%22%5D|host.vars.osfamily=%22redhat%22',
+ self::$exampleHost
+ );
+
+ $this->assertFilterOutcome(
+ false,
+ 'host.vars.operatingsystem=%5B%22centos%22%2C%22fedora%22%5D&(!(host.vars.customer=%22acme*%22))',
+ self::$exampleHost
+ );
+
+ $this->assertFilterOutcome(
+ true,
+ '!(host.vars.bool_string="false")&host.vars.operatingsystem="centos"',
+ self::$exampleHost
+ );
+ }
+
+ /**
+ * @param bool $expected
+ * @param string $filterQuery
+ * @param object $object
+ * @param string $message
+ */
+ protected function assertFilterOutcome($expected, $filterQuery, $object, $message = null, $type = 'host')
+ {
+ $filter = Filter::fromQueryString($filterQuery);
+
+ if ($type === 'host') {
+ HostApplyMatches::fixFilterColumns($filter);
+ }
+
+ $helper = new AssignFilterHelper($filter);
+ $actual = $helper->matches($object);
+
+ if ($message === null) {
+ $message = sprintf('with filter "%s"', $filterQuery);
+ }
+
+ $this->assertEquals($expected, $actual, $message);
+ }
+}
diff --git a/test/php/library/Director/Data/RecursiveUtf8ValidatorTest.php b/test/php/library/Director/Data/RecursiveUtf8ValidatorTest.php
new file mode 100644
index 0000000..1434d4a
--- /dev/null
+++ b/test/php/library/Director/Data/RecursiveUtf8ValidatorTest.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace Tests\Icinga\Module\Director\IcingaConfig;
+
+use Icinga\Module\Director\Data\RecursiveUtf8Validator;
+use Icinga\Module\Director\Test\BaseTestCase;
+
+class RecursiveUtf8ValidatorTest extends BaseTestCase
+{
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testDetectInvalidUtf8Character()
+ {
+ RecursiveUtf8Validator::validateRows([
+ (object) [
+ 'name' => 'test 1',
+ 'value' => 'something',
+ ],
+ (object) [
+ 'name' => 'test 2',
+ 'value' => "some\xa1\xa2thing",
+ ],
+ ]);
+ }
+
+ public function testAcceptValidUtf8Characters()
+ {
+ $this->assertTrue(RecursiveUtf8Validator::validateRows([
+ (object) [
+ 'name' => 'test 1',
+ 'value' => "Some 🍻",
+ ],
+ (object) [
+ 'name' => 'test 2',
+ 'value' => [
+ (object) [
+ 'its' => true,
+ ['💩']
+ ]
+ ],
+ ],
+ ]));
+ }
+}