/** * Import of Manufacturer (with Rule) */ public function testManufacturer() { // Old counters $nbr = countElementsInTable('glpi_rules'); $nba = countElementsInTable('glpi_ruleactions'); $nbc = countElementsInTable('glpi_rulecriterias'); // Create some rules $rule = new RuleDictionnaryDropdown('RuleDictionnaryManufacturer'); $crit = new RuleCriteria(); $acte = new RuleAction(); $idr[0] = $rule->add(array('name' => 'test1', 'sub_type' => 'RuleDictionnaryManufacturer', 'match' => 'AND', 'is_active' => 1)); $this->assertGreaterThan(0, $idr[0], "Fail: can't create rule 1"); $this->assertTrue($rule->getFromDB($idr[0])); $this->assertEquals(1, $rule->fields['ranking'], "Fail: ranking not set"); $idc[0] = $crit->add(array('rules_id' => $idr[0], 'criteria' => 'name', 'condition' => Rule::PATTERN_CONTAIN, 'pattern' => 'indepnet')); $this->assertGreaterThan(0, $idc[0], "Fail: can't create rule 1 criteria"); $ida[0] = $acte->add(array('rules_id' => $idr[0], 'action_type' => 'assign', 'field' => 'name', 'value' => $out1 = 'Indepnet')); $this->assertGreaterThan(0, $ida[0], "Fail: can't create rule 1 action"); // Add another rule $idr[1] = $rule->add(array('name' => 'test2', 'sub_type' => 'RuleDictionnaryManufacturer', 'match' => 'AND', 'is_active' => 1)); $this->assertGreaterThan(0, $idr[1], "Fail: can't create rule 2"); $this->assertTrue($rule->getFromDB($idr[1])); $this->assertEquals(2, $rule->fields['ranking'], "Fail: ranking not set"); $idc[1] = $crit->add(array('rules_id' => $idr[1], 'criteria' => 'name', 'condition' => Rule::PATTERN_BEGIN, 'pattern' => 'http:')); $this->assertGreaterThan(0, $idc[1], "Fail: can't create rule 2 criteria"); $ida[1] = $acte->add(array('rules_id' => $idr[1], 'action_type' => 'assign', 'field' => 'name', 'value' => $out2 = 'Web Site')); $this->assertGreaterThan(0, $ida[1], "Fail: can't create rule 2 action"); $manu = new Manufacturer(); // Import first and fill cache $id[0] = $manu->importExternal($in = 'the indepnet team'); $this->assertGreaterThan(0, $id[0]); $this->assertTrue($manu->getFromDB($id[0])); $this->assertEquals($out1, $manu->fields['name'], "Fail: Rule::PATTERN_CONTAIN not match"); $this->assertEquals(1, countElementsInTable($cache), "Fail: cache empty"); // Import second and use cache $id[1] = $manu->importExternal($in = 'The INDEPNET Team'); $this->assertGreaterThan(0, $id[1]); $this->assertEquals($id[0], $id[1]); $this->assertTrue($manu->getFromDB($id[1])); $this->assertEquals($out1, $manu->fields['name'], "Fail: Rule::PATTERN_CONTAIN not match"); $this->assertEquals(1, countElementsInTable($cache), "Fail: cache not filled"); // Import third not in cache $id[2] = $manu->importExternal($in = 'http://www.indepnet.net/'); $this->assertGreaterThan(0, $id[2]); $this->assertEquals($id[0], $id[2]); $this->assertTrue($manu->getFromDB($id[2])); $this->assertEquals($out1, $manu->fields['name'], "Fail: Rule::PATTERN_CONTAIN not match"); $this->assertEquals(2, countElementsInTable($cache), "Fail: cache not filled"); // Set is_active=0, and clean cache $this->assertTrue($rule->update(array('id' => $idr[0], 'is_active' => 0)), "Fail: update rule"); $this->assertEquals(0, countElementsInTable($cache), "Fail: cache not empty"); $this->assertTrue($rule->update(array('id' => $idr[0], 'is_active' => 1)), "Fail: update rule"); // Import again and fill cache $id[3] = $manu->importExternal($in = 'http://www.glpi-project.org/'); $this->assertGreaterThan(0, $id[3]); $this->assertGreaterThan($id[0], $id[3]); $this->assertTrue($manu->getFromDB($id[3])); $this->assertEquals($out2, $manu->fields['name'], "Fail: Rule::PATTERN_BEGIN not match"); $this->assertEquals(1, countElementsInTable($cache), "Fail: cache empty"); $id[4] = $manu->importExternal($in = 'http://www.indepnet.net/'); $this->assertGreaterThan(0, $id[4]); $this->assertEquals($id[0], $id[4]); $this->assertTrue($manu->getFromDB($id[4])); $this->assertEquals($out1, $manu->fields['name'], "Fail: Rule::PATTERN_CONTAIN not match"); $this->assertEquals(2, countElementsInTable($cache), "Fail: cache not filled"); // Hack : to disable preload done by Singleton $tmp = SingletonRuleList::getInstance('RuleDictionnaryManufacturer'); $tmp->load = 0; // Change rules order $collection = new RuleDictionnaryDropdownCollection('RuleDictionnaryManufacturer'); // Move rule 1 after rule 2 $this->assertTrue($collection->moveRule($idr[0], $idr[1]), "Fail: can't move rules"); $this->assertEquals(0, countElementsInTable($cache), "Fail: cache not empty"); $this->assertTrue($rule->getFromDB($idr[1])); $this->assertEquals(1, $rule->fields['ranking'], "Fail: ranking not change"); $this->assertTrue($rule->getFromDB($idr[0])); $this->assertEquals(2, $rule->fields['ranking'], "Fail: ranking not change"); // Import again and fill cache $id[5] = $manu->importExternal($in = 'http://www.glpi-project.org/'); $this->assertGreaterThan(0, $id[5]); $this->assertTrue($manu->getFromDB($id[5])); $this->assertEquals($out2, $manu->fields['name'], "Fail: Rule::PATTERN_BEGIN not match"); $id[6] = $manu->importExternal($in = 'http://www.indepnet.net/'); $this->assertGreaterThan(0, $id[6]); $this->assertTrue($manu->getFromDB($id[6])); $this->assertEquals($out2, $manu->fields['name'], "Fail: Rule::PATTERN_BEGIN not match"); $this->assertEquals($id[5], $id[6]); // Change rules orders again $tmp = SingletonRuleList::getInstance('RuleDictionnaryManufacturer'); $tmp->load = 0; // Move rule 1 up (before rule 2) $this->assertTrue($collection->changeRuleOrder($idr[0], 'up'), "Fail: can't move rules"); $this->assertEquals(0, countElementsInTable($cache), "Fail: cache not empty"); $this->assertTrue($rule->getFromDB($idr[0])); $this->assertEquals(1, $rule->fields['ranking'], "Fail: ranking not change"); $this->assertTrue($rule->getFromDB($idr[1])); $this->assertEquals(2, $rule->fields['ranking'], "Fail: ranking not change"); // Import again and fill cache $id[7] = $manu->importExternal($in = 'http://www.glpi-project.org/'); $this->assertGreaterThan(0, $id[7]); $this->assertTrue($manu->getFromDB($id[7])); $this->assertEquals($out2, $manu->fields['name'], "Fail: Rule::PATTERN_BEGIN not match"); $id[8] = $manu->importExternal($in = 'http://www.indepnet.net/'); $this->assertGreaterThan(0, $id[8]); $this->assertTrue($manu->getFromDB($id[8])); $this->assertEquals($out1, $manu->fields['name'], "Fail: Rule::PATTERN_CONTAIN not match"); $this->assertNotEquals($id[7], $id[8]); $this->assertEquals(2, countElementsInTable($cache), "Fail: cache not empty"); // Clean $this->assertEquals($nbr + 2, countElementsInTable('glpi_rules'), "Fail: glpi_rules content"); $this->assertEquals($nba + 2, countElementsInTable('glpi_ruleactions'), "Fail: glpi_ruleactions content"); $this->assertEquals($nbc + 2, countElementsInTable('glpi_rulecriterias'), "Fail: glpi_ruleactions content"); $this->assertTrue($rule->delete(array('id' => $idr[0]))); $this->assertEquals(1, countElementsInTable($cache), "Fail: cache not empty"); $this->assertTrue($rule->delete(array('id' => $idr[1]))); $this->assertEquals(0, countElementsInTable($cache), "Fail: cache not empty"); $this->assertEquals($nbr, countElementsInTable('glpi_rules'), "Fail: glpi_rules not empty"); $this->assertEquals($nba, countElementsInTable('glpi_ruleactions'), "Fail: glpi_ruleactions not empty"); $this->assertEquals($nbc, countElementsInTable('glpi_rulecriterias'), "Fail: glpi_ruleactions not empty"); }