/** * @covers NotificationTargetTicket::getTags * @covers NotificationTargetCommonITILObject::getTags * @covers NotificationTargetTicket::getDatasForObject * @covers NotificationTargetCommonITILObject::getDatasForObject */ public function testgetDatasForObject() { global $CFG_GLPI; $tkt = getItemByTypeName('Ticket', '_ticket01'); $notiftargetticket = new NotificationTargetTicket(getItemByTypeName('Entity', '_test_root_entity', true), 'new', $tkt); $notiftargetticket->getTags(); // basic test for ##task.categorycomment## tag $expected = ['tag' => 'task.categorycomment', 'value' => true, 'label' => 'Category comment', 'events' => 0, 'foreach' => false, 'lang' => true, 'allowed_values' => []]; $this->assertEquals($expected, $notiftargetticket->tag_descriptions['lang']['##lang.task.categorycomment##']); $this->assertEquals($expected, $notiftargetticket->tag_descriptions['tag']['##task.categorycomment##']); // basic test for ##task.categorid## tag $expected = ['tag' => 'task.categoryid', 'value' => true, 'label' => 'Category id', 'events' => 0, 'foreach' => false, 'lang' => true, 'allowed_values' => []]; $this->assertEquals($expected, $notiftargetticket->tag_descriptions['lang']['##lang.task.categoryid##']); $this->assertEquals($expected, $notiftargetticket->tag_descriptions['tag']['##task.categoryid##']); // advanced test for ##task.categorycomment## and ##task.categoryid## tags // test of the getDatasForObject for default language en_US $taskcat = getItemByTypeName('TaskCategory', '_subcat_1'); $expected = [['##task.id##' => 1, '##task.isprivate##' => 'No', '##task.author##' => '_test_user', '##task.categoryid##' => $taskcat->getID(), '##task.category##' => '_cat_1 > _subcat_1', '##task.categorycomment##' => 'Comment for sub-category _subcat_1', '##task.date##' => '2016-10-19 11:50', '##task.description##' => 'Task to be done', '##task.time##' => '0 seconds', '##task.status##' => 'To do', '##task.user##' => '_test_user', '##task.group##' => '', '##task.begin##' => '', '##task.end##' => '']]; $ret = $notiftargetticket->getDatasForObject($tkt, array()); $this->assertEquals($expected, $ret['tasks']); // test of the getDatasForObject for default language fr_FR $CFG_GLPI['translate_dropdowns'] = 1; $_SESSION["glpilanguage"] = Session::loadLanguage('fr_FR'); $_SESSION['glpi_dropdowntranslations'] = DropdownTranslation::getAvailableTranslations($_SESSION["glpilanguage"]); $ret = $notiftargetticket->getDatasForObject($tkt, array()); $expected = [['##task.id##' => 1, '##task.isprivate##' => 'Non', '##task.author##' => '_test_user', '##task.categoryid##' => $taskcat->getID(), '##task.category##' => 'FR - _cat_1 > FR - _subcat_1', '##task.categorycomment##' => 'FR - Commentaire pour sous-catégorie _subcat_1', '##task.date##' => '2016-10-19 11:50', '##task.description##' => 'Task to be done', '##task.time##' => '0 seconde', '##task.status##' => 'A faire', '##task.user##' => '_test_user', '##task.group##' => '', '##task.begin##' => '', '##task.end##' => '']]; $this->assertEquals($expected, $ret['tasks']); // switch back to default language $_SESSION["glpilanguage"] = Session::loadLanguage('en_US'); }
function loadDataset() { global $CFG_GLPI; // Unit test data definition $data = ['_version' => 2, 'Entity' => [['name' => '_test_root_entity', 'entities_id' => 0], ['name' => '_test_child_1', 'entities_id' => '_test_root_entity'], ['name' => '_test_child_2', 'entities_id' => '_test_root_entity']], 'Computer' => [['name' => '_test_pc01', 'entities_id' => '_test_root_entity'], ['name' => '_test_pc02', 'entities_id' => '_test_root_entity'], ['name' => '_test_pc11', 'entities_id' => '_test_child_1'], ['name' => '_test_pc12', 'entities_id' => '_test_child_1'], ['name' => '_test_pc21', 'entities_id' => '_test_child_2'], ['name' => '_test_pc22', 'entities_id' => '_test_child_2']], 'Software' => [['name' => '_test_soft', 'entities_id' => '_test_root_entity', 'is_recursive' => 1]], 'SoftwareVersion' => [['name' => '_test_softver_1', 'entities_id' => '_test_root_entity', 'is_recursive' => 1, 'softwares_id' => '_test_soft'], ['name' => '_test_softver_2', 'entities_id' => '_test_root_entity', 'is_recursive' => 1, 'softwares_id' => '_test_soft']], 'Printer' => [['name' => '_test_printer_all', 'entities_id' => '_test_root_entity', 'is_recursive' => 1], ['name' => '_test_printer_ent0', 'entities_id' => '_test_root_entity', 'is_recursive' => 0], ['name' => '_test_printer_ent1', 'entities_id' => '_test_child_1', 'is_recursive' => 0], ['name' => '_test_printer_ent2', 'entities_id' => '_test_child_2', 'is_recursive' => 0]], 'User' => [['name' => TU_USER, 'password' => TU_PASS, 'password2' => TU_PASS, 'entities_id' => '_test_root_entity', 'profiles_id' => 4, '_entities_id' => '_test_root_entity', '_profiles_id' => 4, '_is_recursive' => 1]]]; // To bypass various right checks $_SESSION['glpicronuserrunning'] = "cron_phpunit"; $_SESSION['glpi_use_mode'] = Session::NORMAL_MODE; $CFG_GLPI['root_doc'] = '/glpi'; // need to set theses in DB, because tests for API use http call and this bootstrap file is not called Config::setConfigurationValues('core', ['url_base' => GLPI_URI, 'url_base_api' => GLPI_URI . '/apirest.php']); $CFG_GLPI['url_base'] = GLPI_URI; $CFG_GLPI['url_base_api'] = GLPI_URI . '/apirest.php'; @mkdir(GLPI_LOG_DIR, 0755, true); $conf = Config::getConfigurationValues('phpunit'); if (isset($conf['dataset']) && $conf['dataset'] == $data['_version']) { printf("\nGLPI dataset version %d already loaded\n\n", $data['_version']); } else { printf("\nLoading GLPI dataset version %d\n", $data['_version']); $ids = array(); foreach ($data as $type => $inputs) { if ($type[0] == '_') { continue; } foreach ($inputs as $input) { // Resolve FK foreach ($input as $k => $v) { if (isForeignKeyField($k) && isset($ids[$v]) && !is_numeric($v)) { $input[$k] = $ids[$v]; } } if (isset($input['name']) && ($item = getItemByTypeName($type, $input['name']))) { $input['id'] = $ids[$input['name']] = $item->getField('id'); $item->update($input); echo "."; } else { // Not found, create it $item = getItemForItemtype($type); $id = $item->add($input); echo "+"; if (isset($input['name'])) { $ids[$input['name']] = $id; } } } } echo "\nDone\n\n"; Config::setConfigurationValues('phpunit', ['dataset' => $data['_version']]); } }
/** * @covers NetworkPort::prepareInputForAdd * @covers NetworkPort::post_addItem */ public function testAddCompleteNetworkPort() { $this->Login(); $computer1 = getItemByTypeName('Computer', '_test_pc01'); // Do some installations $ins = new NetworkPort(); // Be sure added $nb_log = countElementsInTable('glpi_logs'); $this->assertGreaterThan(0, $ins->add(['items_id' => $computer1->getID(), 'itemtype' => 'Computer', 'entities_id' => $computer1->fields['entities_id'], 'is_recursive' => 0, 'logical_number' => 3, 'mac' => '00:24:81:eb:c6:d2', 'instantiation_type' => 'NetworkPortEthernet', 'name' => 'em3', 'comment' => 'Comment me!', 'netpoints_id' => 0, 'items_devicenetworkcards_id' => 0, 'type' => 'T', 'speed' => 1000, 'speed_other_value' => '', 'NetworkName_name' => 'test1.me', 'NetworkName_fqdns_id' => 0, 'NetworkName__ipaddresses' => ['-1' => '192.168.20.1']])); $this->assertGreaterThan($nb_log, countElementsInTable('glpi_logs')); // be sure added and have no logs $nb_log = countElementsInTable('glpi_logs'); $this->assertGreaterThan(0, $ins->add(['items_id' => $computer1->getID(), 'itemtype' => 'Computer', 'entities_id' => $computer1->fields['entities_id'], 'is_recursive' => 0, 'logical_number' => 4, 'mac' => '00:24:81:eb:c6:d4', 'instantiation_type' => 'NetworkPortEthernet', 'name' => 'em4', 'comment' => 'Comment me!', 'netpoints_id' => 0, 'items_devicenetworkcards_id' => 0, 'type' => 'T', 'speed' => 1000, 'speed_other_value' => '', 'NetworkName_name' => 'test2.me', 'NetworkName_fqdns_id' => 0, 'NetworkName__ipaddresses' => ['-1' => '192.168.20.2']], [], false)); $this->assertEquals($nb_log, countElementsInTable('glpi_logs')); }
/** * @covers Computer::post_updateItem */ public function testTypeName() { global $CFG_GLPI; $computer = getItemByTypeName('Computer', '_test_pc01'); $savecomp = $computer->fields; $saveconf = $CFG_GLPI; $printer = getItemByTypeName('Printer', '_test_printer_all'); // Create the link $link = new Computer_Item(); $in = ['computers_id' => $computer->getField('id'), 'itemtype' => $printer->getType(), 'items_id' => $printer->getID()]; $this->assertGreaterThan(0, $link->add($in)); // Change the computer $CFG_GLPI['is_contact_autoupdate'] = 1; $CFG_GLPI['is_user_autoupdate'] = 1; $CFG_GLPI['is_group_autoupdate'] = 1; $CFG_GLPI['state_autoupdate_mode'] = -1; $CFG_GLPI['is_location_autoupdate'] = 1; $in = ['id' => $computer->getField('id'), 'contact' => $this->getUniqueString(), 'contact_num' => $this->getUniqueString(), 'users_id' => $this->getUniqueInteger(), 'groups_id' => $this->getUniqueInteger(), 'states_id' => $this->getUniqueInteger(), 'locations_id' => $this->getUniqueInteger()]; $this->assertTrue($computer->update($in)); $this->assertTrue($computer->getFromDB($computer->getID())); $this->assertTrue($printer->getFromDB($printer->getID())); foreach ($in as $k => $v) { // Check the computer new values $this->assertEquals($v, $computer->getField($k), $k); // Check the printer and test propagation occurs $this->assertEquals($v, $printer->getField($k), $k); } // Change the computer again $CFG_GLPI['is_contact_autoupdate'] = 0; $CFG_GLPI['is_user_autoupdate'] = 0; $CFG_GLPI['is_group_autoupdate'] = 0; $CFG_GLPI['state_autoupdate_mode'] = 0; $CFG_GLPI['is_location_autoupdate'] = 0; $in2 = ['id' => $computer->getField('id'), 'contact' => $this->getUniqueString(), 'contact_num' => $this->getUniqueString(), 'users_id' => $this->getUniqueInteger(), 'groups_id' => $this->getUniqueInteger(), 'states_id' => $this->getUniqueInteger(), 'locations_id' => $this->getUniqueInteger()]; $this->assertTrue($computer->update($in2)); $this->assertTrue($computer->getFromDB($computer->getID())); $this->assertTrue($printer->getFromDB($printer->getID())); foreach ($in2 as $k => $v) { // Check the computer new values $this->assertEquals($v, $computer->getField($k), $k); // Check the printer and test propagation DOES NOT occurs $this->assertEquals($in[$k], $printer->getField($k), $k); } // Restore state $computer->update($savecomp); // Restore configuration $CFG_GLPI = $saveconf; }
/** * @covers NetworkPort::prepareInputForAdd * @covers NetworkPort::post_addItem */ public function testAddCompleteNetworkPort() { $this->Login(); $computer1 = getItemByTypeName('Computer', '_test_pc01'); // Do some installations $networkport = new NetworkPort(); // Be sure added $nb_log = countElementsInTable('glpi_logs'); $new_id = $networkport->add(['items_id' => $computer1->getID(), 'itemtype' => 'Computer', 'entities_id' => $computer1->fields['entities_id'], 'is_recursive' => 0, 'logical_number' => 3, 'mac' => '00:24:81:eb:c6:d2', 'instantiation_type' => 'NetworkPortEthernet', 'name' => 'em3', 'comment' => 'Comment me!', 'netpoints_id' => 0, 'items_devicenetworkcards_id' => 0, 'type' => 'T', 'speed' => 1000, 'speed_other_value' => '', 'NetworkName_name' => 'test1', 'NetworkName_comment' => 'test1 comment', 'NetworkName_fqdns_id' => 0, 'NetworkName__ipaddresses' => ['-1' => '192.168.20.1'], '_create_children' => true]); $this->assertGreaterThan(0, $new_id); $this->assertGreaterThan($nb_log, countElementsInTable('glpi_logs')); // check data in db // 1 -> NetworkPortEthernet $all_netportethernets = getAllDatasFromTable('glpi_networkportethernets', '', false, 'id'); $networkportethernet = end($all_netportethernets); unset($networkportethernet['id']); unset($networkportethernet['date_mod']); unset($networkportethernet['date_creation']); $expected = ['networkports_id' => $new_id, 'items_devicenetworkcards_id' => '0', 'netpoints_id' => '0', 'type' => 'T', 'speed' => '1000']; $this->assertEquals($expected, $networkportethernet); // 2 -> NetworkName $all_networknames = getAllDatasFromTable('glpi_networknames', '', false, 'id'); $networkname = end($all_networknames); $networknames_id = $networkname['id']; unset($networkname['id']); unset($networkname['date_mod']); unset($networkname['date_creation']); $expected = ['entities_id' => $computer1->fields['entities_id'], 'items_id' => $new_id, 'itemtype' => 'NetworkPort', 'name' => 'test1', 'comment' => 'test1 comment', 'fqdns_id' => 0, 'is_deleted' => 0, 'is_dynamic' => 0]; $this->assertEquals($expected, $networkname); // 3 -> IPAddress $all_ipadresses = getAllDatasFromTable('glpi_ipaddresses', '', false, 'id'); $ipadress = end($all_ipadresses); unset($ipadress['id']); unset($ipadress['date_mod']); unset($ipadress['date_creation']); $expected = ['entities_id' => $computer1->fields['entities_id'], 'items_id' => $networknames_id, 'itemtype' => 'NetworkName', 'version' => '4', 'name' => '192.168.20.1', 'binary_0' => '0', 'binary_1' => '0', 'binary_2' => '65535', 'binary_3' => '3232240641', 'is_deleted' => '0', 'is_dynamic' => '0', 'mainitems_id' => $computer1->getID(), 'mainitemtype' => 'Computer']; $this->assertEquals($expected, $ipadress); // be sure added and have no logs $nb_log = countElementsInTable('glpi_logs'); $new_id = $networkport->add(['items_id' => $computer1->getID(), 'itemtype' => 'Computer', 'entities_id' => $computer1->fields['entities_id'], 'is_recursive' => 0, 'logical_number' => 4, 'mac' => '00:24:81:eb:c6:d4', 'instantiation_type' => 'NetworkPortEthernet', 'name' => 'em4', 'comment' => 'Comment me!', 'netpoints_id' => 0, 'items_devicenetworkcards_id' => 0, 'type' => 'T', 'speed' => 1000, 'speed_other_value' => '', 'NetworkName_name' => 'test2', 'NetworkName_fqdns_id' => 0, 'NetworkName__ipaddresses' => ['-1' => '192.168.20.2']], [], false); $this->assertGreaterThan(0, $new_id); $this->assertEquals($nb_log, countElementsInTable('glpi_logs')); }
public function testAddDelete() { $alert = new Alert(); $nb = countElementsInTable($alert->getTable()); $comp = getItemByTypeName('Computer', '_test_pc01'); $date = '2016-09-01 12:34:56'; // Add $id = $alert->add(['itemtype' => $comp->getType(), 'items_id' => $comp->getID(), 'type' => Alert::END, 'date' => $date]); $this->assertGreaterThan(0, $id); $this->assertGreaterThan($nb, countElementsInTable($alert->getTable())); // Getters $this->assertFalse(Alert::alertExists($comp->getType(), $comp->getID(), Alert::NOTICE)); $this->assertEquals($id, Alert::alertExists($comp->getType(), $comp->getID(), Alert::END)); $this->assertEquals($date, Alert::getAlertDate($comp->getType(), $comp->getID(), Alert::END)); // Delete $this->assertTrue($alert->clear($comp->getType(), $comp->getID(), Alert::END)); $this->assertEquals($nb, countElementsInTable($alert->getTable())); // Still true, nothing to delete but no error $this->assertTrue($alert->clear($comp->getType(), $comp->getID(), Alert::END)); }
/** * @covers ::getSonsOf * @covers ::getAncestorsOf */ public function testSonsAncestors() { $ent0 = getItemByTypeName('Entity', '_test_root_entity'); $this->assertEquals('Root entity > _test_root_entity', $ent0->getField('completename')); $ent1 = getItemByTypeName('Entity', '_test_child_1'); $this->assertEquals('Root entity > _test_root_entity > _test_child_1', $ent1->getField('completename')); $ent2 = getItemByTypeName('Entity', '_test_child_2'); $this->assertEquals('Root entity > _test_root_entity > _test_child_2', $ent2->getField('completename')); $this->assertEquals([0], array_keys(getAncestorsOf('glpi_entities', $ent0->getID()))); $this->assertEquals([0], array_values(getAncestorsOf('glpi_entities', $ent0->getID()))); $this->assertEquals([$ent0->getID(), $ent1->getID(), $ent2->getID()], array_keys(getSonsOf('glpi_entities', $ent0->getID()))); $this->assertEquals([$ent0->getID(), $ent1->getID(), $ent2->getID()], array_values(getSonsOf('glpi_entities', $ent0->getID()))); $this->assertEquals([0, $ent0->getID()], array_keys(getAncestorsOf('glpi_entities', $ent1->getID()))); $this->assertEquals([0, $ent0->getID()], array_values(getAncestorsOf('glpi_entities', $ent1->getID()))); $this->assertEquals([$ent1->getID()], array_keys(getSonsOf('glpi_entities', $ent1->getID()))); $this->assertEquals([$ent1->getID()], array_values(getSonsOf('glpi_entities', $ent1->getID()))); $this->assertEquals([0, $ent0->getID()], array_keys(getAncestorsOf('glpi_entities', $ent2->getID()))); $this->assertEquals([0, $ent0->getID()], array_values(getAncestorsOf('glpi_entities', $ent2->getID()))); $this->assertEquals([$ent2->getID()], array_keys(getSonsOf('glpi_entities', $ent2->getID()))); $this->assertEquals([$ent2->getID()], array_values(getSonsOf('glpi_entities', $ent2->getID()))); }
/** * @covers ::countElementsInTableForEntity */ public function testCountElementsInTableForEntity() { $e = getItemByTypeName('Entity', '_test_child_1', true); $this->assertEquals(2, countElementsInTableForEntity('glpi_computers', $e)); $this->assertEquals(1, countElementsInTableForEntity('glpi_computers', $e, 'name="_test_pc11"')); $this->assertEquals(0, countElementsInTableForEntity('glpi_computers', $e, 'name="_test_pc01"')); $e = getItemByTypeName('Entity', '_test_root_entity', true); $this->assertEquals(1, countElementsInTableForEntity('glpi_computers', $e, 'name="_test_pc01"')); }
/** * @depends testInitSessionCredentials * @depends testCreateItem */ public function testGetItem($session_token, $computers_id) { // Get the User TU_USER $uid = getItemByTypeName('User', TU_USER, true); $res = $this->doHttpRequest('GET', "User/{$uid}/", ['headers' => ['Session-Token' => $session_token], 'query' => ['expand_dropdowns' => true, 'with_logs' => true]]); $this->assertNotEquals(null, $res, $this->last_error); $this->assertEquals(200, $res->getStatusCode()); $body = $res->getBody(); $data = json_decode($body, true); $this->assertArrayHasKey('id', $data); $this->assertArrayHasKey('name', $data); $this->assertArrayHasKey('entities_id', $data); $this->assertArrayHasKey('links', $data); $this->assertArrayNotHasKey('password', $data); $this->assertFalse(is_numeric($data['entities_id'])); // for expand_dropdowns $this->assertArrayHasKey('_logs', $data); // with_logs == true // Get user's entity $eid = getItemByTypeName('Entity', '_test_root_entity', true); $res = $this->doHttpRequest('GET', 'Entity/' . $eid, ['headers' => ['Session-Token' => $session_token], 'query' => ['get_hateoas' => false]]); $this->assertNotEquals(null, $res, $this->last_error); $this->assertEquals(200, $res->getStatusCode()); $body = $res->getBody(); $data = json_decode($body, true); $this->assertNotEquals(false, $data); $this->assertArrayHasKey('id', $data); $this->assertArrayHasKey('name', $data); $this->assertArrayHasKey('completename', $data); $this->assertArrayNotHasKey('links', $data); // get_hateoas == false // Get the previously created 'computer 1' $res = $this->doHttpRequest('GET', "Computer/{$computers_id}", ['headers' => ['Session-Token' => $session_token], 'query' => ['with_networkports' => true]]); $this->assertNotEquals(null, $res, $this->last_error); $this->assertEquals(200, $res->getStatusCode()); $body = $res->getBody(); $data = json_decode($body, true); $this->assertNotEquals(false, $data); $this->assertArrayHasKey('id', $data); $this->assertArrayHasKey('name', $data); $this->assertArrayHasKey('_networkports', $data); $this->assertArrayHasKey('NetworkName', $data['_networkports']['NetworkPortEthernet'][0]); $networkname = $data['_networkports']['NetworkPortEthernet'][0]['NetworkName']; $this->assertArrayHasKey('IPAddress', $networkname); $this->assertArrayHasKey('FQDN', $networkname); $this->assertArrayHasKey('id', $networkname['IPAddress'][0]); $this->assertArrayHasKey('name', $networkname['IPAddress'][0]); $this->assertArrayHasKey('IPNetwork', $networkname['IPAddress'][0]); $this->assertEquals('1.2.3.4', $networkname['IPAddress'][0]['name']); }
/** * @covers Computer_SoftwareVersion::updateDatasForComputer */ public function testUpdateDatasFromComputer() { $c00 = 1566671; $computer1 = getItemByTypeName('Computer', '_test_pc01'); $ver1 = getItemByTypeName('SoftwareVersion', '_test_softver_1', true); $ver2 = getItemByTypeName('SoftwareVersion', '_test_softver_2', true); // Do some installations $softver = new Computer_SoftwareVersion(); $softver01 = $softver->add(['computers_id' => $computer1->getID(), 'softwareversions_id' => $ver1]); $this->assertGreaterThan(0, $softver01); $softver02 = $softver->add(['computers_id' => $computer1->getID(), 'softwareversions_id' => $ver2]); $this->assertGreaterThan(0, $softver02); foreach ([$softver01, $softver02] as $tsoftver) { $o = new Computer_SoftwareVersion(); $o->getFromDb($tsoftver); $this->assertEquals('0', $o->getField('is_deleted_computer')); } //computer that does not exists $this->assertFalse($softver->updateDatasForComputer($c00)); //update existing computer $input = $computer1->fields; $input['is_deleted'] = '1'; $this->assertTrue($computer1->update($input)); $this->assertEquals(2, $softver->updateDatasForComputer($computer1->getID())); //check if all has been updated foreach ([$softver01, $softver02] as $tsoftver) { $o = new Computer_SoftwareVersion(); $o->getFromDb($tsoftver); $this->assertEquals('1', $o->getField('is_deleted_computer')); } //restore computer state $input['is_deleted'] = '0'; $this->assertTrue($computer1->update($input)); }
/** * @covers Printer::deleteByCriteria * @covers Printer::restore */ public function testDeleteByCriteria() { $obj = new Printer(); $this->assertTrue($obj->maybeDeleted()); // Add $id = $obj->add(['name' => __METHOD__, 'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true)]); $this->assertGreaterThan(0, $id); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(0, $obj->getField('is_deleted')); $this->assertEquals(0, $obj->isDeleted()); $nb_before = countElementsInTable('glpi_logs', "itemtype = 'Printer'\n AND items_id = '{$id}'"); // DeleteByCriteria without history $this->assertTrue($obj->deleteByCriteria(['name' => __METHOD__], 0, 0)); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(1, $obj->getField('is_deleted')); $this->assertEquals(1, $obj->isDeleted()); $nb_after = countElementsInTable('glpi_logs', "itemtype = 'Printer'\n AND items_id = '{$id}'"); $this->assertEquals($nb_before, $nb_after); // Restore $this->assertTrue($obj->restore(['id' => $id], 0)); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(0, $obj->getField('is_deleted')); $this->assertEquals(0, $obj->isDeleted()); $nb_before = countElementsInTable('glpi_logs', "itemtype = 'Printer'\n AND items_id = '{$id}'"); // DeleteByCriteria with history $this->assertTrue($obj->deleteByCriteria(['name' => __METHOD__], 0, 1)); $this->assertTrue($obj->getFromDB($id)); $this->assertEquals(1, $obj->getField('is_deleted')); $this->assertEquals(1, $obj->isDeleted()); $nb_after = countElementsInTable('glpi_logs', "itemtype = 'Printer'\n AND items_id = '{$id}'"); $this->assertEquals($nb_before + 1, $nb_after); }
/** * @covers Dropdown::getDropdownName * @covers ::getTreeValueCompleteName */ public function testGetDropdownName() { global $CFG_GLPI; $cat = getItemByTypeName('TaskCategory', '_cat_1'); $subCat = getItemByTypeName('TaskCategory', '_subcat_1'); // basic test returns string only $expected = $cat->fields['name'] . " > " . $subCat->fields['name']; $ret = Dropdown::getDropdownName('glpi_taskcategories', $subCat->getID()); $this->assertEquals($expected, $ret); // test of return with comments $expected = array('name' => $cat->fields['name'] . " > " . $subCat->fields['name'], 'comment' => "<span class='b'>Complete name</span>: " . $cat->fields['name'] . " > " . $subCat->fields['name'] . "<br><span class='b'> Comments </span>" . $subCat->fields['comment']); $ret = Dropdown::getDropdownName('glpi_taskcategories', $subCat->getID(), true); $this->assertEquals($expected, $ret); // test of return without $tooltip $expected = array('name' => $cat->fields['name'] . " > " . $subCat->fields['name'], 'comment' => $subCat->fields['comment']); $ret = Dropdown::getDropdownName('glpi_taskcategories', $subCat->getID(), true, true, false); $this->assertEquals($expected, $ret); // test of return with translations $CFG_GLPI['translate_dropdowns'] = 1; $_SESSION["glpilanguage"] = Session::loadLanguage('fr_FR'); $_SESSION['glpi_dropdowntranslations'] = DropdownTranslation::getAvailableTranslations($_SESSION["glpilanguage"]); $expected = array('name' => 'FR - _cat_1 > FR - _subcat_1', 'comment' => 'FR - Commentaire pour sous-catégorie _subcat_1'); $ret = Dropdown::getDropdownName('glpi_taskcategories', $subCat->getID(), true, true, false); $this->assertEquals($expected, $ret); // switch back to default language $_SESSION["glpilanguage"] = Session::loadLanguage('en_US'); //////////////////////////////// // test for other dropdown types //////////////////////////////// /////////// // Computer $computer = getItemByTypeName('Computer', '_test_pc01'); $ret = Dropdown::getDropdownName('glpi_computers', $computer->getID()); $this->assertEquals($computer->getName(), $ret); $expected = array('name' => $computer->getName(), 'comment' => $computer->fields['comment']); $ret = Dropdown::getDropdownName('glpi_computers', $computer->getID(), true); $this->assertEquals($expected, $ret); ////////// // Contact $contact = getItemByTypeName('Contact', '_contact01_name'); $expected = $contact->getName(); $ret = Dropdown::getDropdownName('glpi_contacts', $contact->getID()); $this->assertEquals($expected, $ret); // test of return with comments $expected = array('name' => $contact->getName(), 'comment' => "Comment for contact _contact01_name<br><span class='b'>" . "Phone: </span>0123456789<br><span class='b'>Phone 2: </span>0123456788<br><span class='b'>" . "Mobile phone: </span>0623456789<br><span class='b'>Fax: </span>0123456787<br>" . "<span class='b'>Email: </span>_contact01_firstname._contact01_name@glpi.com"); $ret = Dropdown::getDropdownName('glpi_contacts', $contact->getID(), true); $this->assertEquals($expected, $ret); // test of return without $tooltip $expected = array('name' => $contact->getName(), 'comment' => $contact->fields['comment']); $ret = Dropdown::getDropdownName('glpi_contacts', $contact->getID(), true, true, false); $this->assertEquals($expected, $ret); /////////// // Supplier $supplier = getItemByTypeName('Supplier', '_suplier01_name'); $expected = $supplier->getName(); $ret = Dropdown::getDropdownName('glpi_suppliers', $supplier->getID()); $this->assertEquals($expected, $ret); // test of return with comments $expected = array('name' => $supplier->getName(), 'comment' => "Comment for supplier _suplier01_name<br><span class='b'>Phone: </span>0123456789<br>" . "<span class='b'>Fax: </span>0123456787<br><span class='b'>Email: </span>info@_supplier01_name.com"); $ret = Dropdown::getDropdownName('glpi_suppliers', $supplier->getID(), true); $this->assertEquals($expected, $ret); // test of return without $tooltip $expected = array('name' => $supplier->getName(), 'comment' => $supplier->fields['comment']); $ret = Dropdown::getDropdownName('glpi_suppliers', $supplier->getID(), true, true, false); $this->assertEquals($expected, $ret); /////////// // Netpoint $netpoint = getItemByTypeName('Netpoint', '_netpoint01'); $location = getItemByTypeName('Location', '_location01'); $expected = $netpoint->getName() . " (" . $location->getName() . ")"; $ret = Dropdown::getDropdownName('glpi_netpoints', $netpoint->getID()); $this->assertEquals($expected, $ret); // test of return with comments $expected = array('name' => $expected, 'comment' => "Comment for netpoint _netpoint01"); $ret = Dropdown::getDropdownName('glpi_netpoints', $netpoint->getID(), true); $this->assertEquals($expected, $ret); // test of return without $tooltip $ret = Dropdown::getDropdownName('glpi_netpoints', $netpoint->getID(), true, true, false); $this->assertEquals($expected, $ret); /////////// // Budget $budget = getItemByTypeName('Budget', '_budget01'); $expected = $budget->getName(); $ret = Dropdown::getDropdownName('glpi_budgets', $budget->getID()); $this->assertEquals($expected, $ret); // test of return with comments $expected = array('name' => $budget->getName(), 'comment' => "Comment for budget _budget01<br><span class='b'>Location</span>: " . "_location01<br><span class='b'>Type</span>: _budgettype01<br><span class='b'>" . "Start date</span>: 2016-10-18 <br><span class='b'>End date</span>: 2016-12-31 "); $ret = Dropdown::getDropdownName('glpi_budgets', $budget->getID(), true); $this->assertEquals($expected, $ret); // test of return without $tooltip $expected = array('name' => $budget->getName(), 'comment' => $budget->fields['comment']); $ret = Dropdown::getDropdownName('glpi_budgets', $budget->getID(), true, true, false); $this->assertEquals($expected, $ret); }
function loadDataset() { global $CFG_GLPI; // Unit test data definition $data = ['_version' => 3, 'Entity' => [['name' => '_test_root_entity', 'entities_id' => 0], ['name' => '_test_child_1', 'entities_id' => '_test_root_entity'], ['name' => '_test_child_2', 'entities_id' => '_test_root_entity']], 'Computer' => [['name' => '_test_pc01', 'entities_id' => '_test_root_entity', 'comment' => 'Comment for computer _test_pc01'], ['name' => '_test_pc02', 'entities_id' => '_test_root_entity', 'comment' => 'Comment for computer _test_pc02'], ['name' => '_test_pc11', 'entities_id' => '_test_child_1'], ['name' => '_test_pc12', 'entities_id' => '_test_child_1'], ['name' => '_test_pc21', 'entities_id' => '_test_child_2'], ['name' => '_test_pc22', 'entities_id' => '_test_child_2']], 'Software' => [['name' => '_test_soft', 'entities_id' => '_test_root_entity', 'is_recursive' => 1]], 'SoftwareVersion' => [['name' => '_test_softver_1', 'entities_id' => '_test_root_entity', 'is_recursive' => 1, 'softwares_id' => '_test_soft'], ['name' => '_test_softver_2', 'entities_id' => '_test_root_entity', 'is_recursive' => 1, 'softwares_id' => '_test_soft']], 'Printer' => [['name' => '_test_printer_all', 'entities_id' => '_test_root_entity', 'is_recursive' => 1], ['name' => '_test_printer_ent0', 'entities_id' => '_test_root_entity', 'is_recursive' => 0], ['name' => '_test_printer_ent1', 'entities_id' => '_test_child_1', 'is_recursive' => 0], ['name' => '_test_printer_ent2', 'entities_id' => '_test_child_2', 'is_recursive' => 0]], 'User' => [['name' => TU_USER, 'password' => TU_PASS, 'password2' => TU_PASS, 'entities_id' => '_test_root_entity', 'profiles_id' => 4, '_entities_id' => '_test_root_entity', '_profiles_id' => 4, '_is_recursive' => 1]], 'TaskCategory' => [['is_recursive' => 1, 'name' => '_cat_1', 'completename' => '_cat_1', 'comment' => 'Comment for category _cat_1', 'level' => 1], ['is_recursive' => 1, 'taskcategories_id' => '_cat_1', 'name' => '_subcat_1', 'completename' => '_cat_1 > _subcat_1', 'comment' => 'Comment for sub-category _subcat_1', 'level' => 2]], 'DropdownTranslation' => [['items_id' => '_cat_1', 'itemtype' => 'TaskCategory', 'language' => 'fr_FR', 'field' => 'name', 'value' => 'FR - _cat_1'], ['items_id' => '_cat_1', 'itemtype' => 'TaskCategory', 'language' => 'fr_FR', 'field' => 'comment', 'value' => 'FR - Commentaire pour catégorie _cat_1'], ['items_id' => '_subcat_1', 'itemtype' => 'TaskCategory', 'language' => 'fr_FR', 'field' => 'name', 'value' => 'FR - _subcat_1'], ['items_id' => '_subcat_1', 'itemtype' => 'TaskCategory', 'language' => 'fr_FR', 'field' => 'comment', 'value' => 'FR - Commentaire pour sous-catégorie _subcat_1']], 'Contact' => [['name' => '_contact01_name', 'firstname' => '_contact01_firstname', 'phone' => '0123456789', 'phone2' => '0123456788', 'mobile' => '0623456789', 'fax' => '0123456787', 'email' => '*****@*****.**', 'comment' => 'Comment for contact _contact01_name']], 'Supplier' => [['name' => '_suplier01_name', 'phonenumber' => '0123456789', 'fax' => '0123456787', 'email' => 'info@_supplier01_name.com', 'comment' => 'Comment for supplier _suplier01_name']], 'Location' => [['name' => '_location01', 'comment' => 'Comment for location _location01']], 'Netpoint' => [['name' => '_netpoint01', 'locations_id' => '_location01', 'comment' => 'Comment for netpoint _netpoint01']], 'BudgetType' => [['name' => '_budgettype01', 'comment' => 'Comment for budgettype _budgettype01']], 'Budget' => [['name' => '_budget01', 'comment' => 'Comment for budget _budget01', 'locations_id' => '_location01', 'budgettypes_id' => '_budgettype01', 'begin_date' => '2016-10-18', 'end_date' => '2016-12-31']], 'Ticket' => [['name' => '_ticket01', 'content' => 'Content for ticket _ticket01', 'users_id_recipient' => TU_USER]], 'TicketTask' => [['tickets_id' => '_ticket01', 'taskcategories_id' => '_subcat_1', 'users_id' => TU_USER, 'content' => 'Task to be done', 'is_private' => 0, 'users_id_tech' => TU_USER, 'date' => '2016-10-19 11:50:50']], 'UserEmail' => [['users_id' => TU_USER, 'is_default' => '1', 'is_dynamic' => '0', 'email' => TU_USER . '@glpi.com']]]; // To bypass various right checks $_SESSION['glpishowallentities'] = 1; $_SESSION['glpicronuserrunning'] = "cron_phpunit"; $_SESSION['glpi_use_mode'] = Session::NORMAL_MODE; $CFG_GLPI['root_doc'] = '/glpi'; // need to set theses in DB, because tests for API use http call and this bootstrap file is not called Config::setConfigurationValues('core', ['url_base' => GLPI_URI, 'url_base_api' => GLPI_URI . '/apirest.php']); $CFG_GLPI['url_base'] = GLPI_URI; $CFG_GLPI['url_base_api'] = GLPI_URI . '/apirest.php'; is_dir(GLPI_LOG_DIR) or mkdir(GLPI_LOG_DIR, 0755, true); $conf = Config::getConfigurationValues('phpunit'); if (isset($conf['dataset']) && $conf['dataset'] == $data['_version']) { printf("\nGLPI dataset version %d already loaded\n\n", $data['_version']); } else { printf("\nLoading GLPI dataset version %d\n", $data['_version']); $ids = array(); foreach ($data as $type => $inputs) { if ($type[0] == '_') { continue; } foreach ($inputs as $input) { // Resolve FK foreach ($input as $k => $v) { // $foreigntype = $type; // by default same type than current type (is the case of the dropdowns) $foreigntype = false; $match = array(); if (isForeignKeyField($k) && (preg_match("/(.*s)_id\$/", $k, $match) || preg_match("/(.*s)_id_/", $k, $match))) { $foreigntype = array_pop($match); $foreigntype = getItemTypeForTable("glpi_{$foreigntype}"); } if ($foreigntype && isset($ids[$foreigntype][$v]) && !is_numeric($v)) { $input[$k] = $ids[$foreigntype][$v]; } else { if ($k == 'items_id' && isset($input['itemtype']) && isset($ids[$input['itemtype']][$v]) && !is_numeric($v)) { $input[$k] = $ids[$input['itemtype']][$v]; } else { if ($foreigntype && $foreigntype != 'UNKNOWN' && !is_numeric($v)) { // not found in ids array, then must get it from DB if ($obj = getItemByTypeName($foreigntype, $v)) { $input[$k] = $obj->getID(); } } } } } if (isset($input['name']) && ($item = getItemByTypeName($type, $input['name']))) { $input['id'] = $ids[$type][$input['name']] = $item->getField('id'); $item->update($input); echo "."; } else { // Not found, create it $item = getItemForItemtype($type); $id = $item->add($input); echo "+"; if (isset($input['name'])) { $ids[$type][$input['name']] = $id; } } } } echo "\nDone\n\n"; Config::setConfigurationValues('phpunit', ['dataset' => $data['_version']]); } }
/** * @depends testInitSessionCredentials */ public function testGetMultipleItems($session_token) { // Get the User TU_USER and the its entity in the same query $uid = getItemByTypeName('User', TU_USER, true); $eid = getItemByTypeName('Entity', '_test_root_entity', true); $res = $this->doHttpRequest('getMultipleItems', ['session_token' => $session_token, 'items' => [['itemtype' => 'User', 'items_id' => $uid], ['itemtype' => 'Entity', 'items_id' => $eid]], 'expand_dropdowns' => true, 'with_logs' => true]); $this->assertEquals(200, $res->getStatusCode()); $data = xmlrpc_decode($res->getBody()); $this->assertEquals(true, is_array($data)); $this->assertEquals(2, count($data)); foreach ($data as $item) { $this->assertArrayHasKey('id', $item); $this->assertArrayHasKey('name', $item); $this->assertArrayHasKey('entities_id', $item); $this->assertArrayNotHasKey('password', $item); $this->assertArrayHasKey('links', $item); $this->assertFalse(is_numeric($item['entities_id'])); // for expand_dropdowns $this->assertArrayHasKey('_logs', $item); // with_logs == true } }
/** * change current entity */ protected function setEntity($entityname, $subtree) { $this->assertTrue(Session::changeActiveEntities(getItemByTypeName('Entity', $entityname, true), $subtree)); }
public function testVisibility() { $this->Login(); $p = new Printer(); // Visibility from root + tree $this->setEntity('_test_root_entity', true); $this->assertTrue($p->can(getItemByTypeName('Printer', '_test_printer_all', true), READ)); $this->assertTrue($p->can(getItemByTypeName('Printer', '_test_printer_ent0', true), READ)); $this->assertTrue($p->can(getItemByTypeName('Printer', '_test_printer_ent1', true), READ)); $this->assertTrue($p->can(getItemByTypeName('Printer', '_test_printer_ent2', true), READ)); // Visibility from root only $this->setEntity('_test_root_entity', false); $this->assertTrue($p->can(getItemByTypeName('Printer', '_test_printer_all', true), READ)); $this->assertTrue($p->can(getItemByTypeName('Printer', '_test_printer_ent0', true), READ)); $this->assertFalse($p->can(getItemByTypeName('Printer', '_test_printer_ent1', true), READ)); $this->assertFalse($p->can(getItemByTypeName('Printer', '_test_printer_ent2', true), READ)); // Visibility from child $this->setEntity('_test_child_1', false); $this->assertTrue($p->can(getItemByTypeName('Printer', '_test_printer_all', true), READ)); $this->assertFalse($p->can(getItemByTypeName('Printer', '_test_printer_ent0', true), READ)); $this->assertTrue($p->can(getItemByTypeName('Printer', '_test_printer_ent1', true), READ)); $this->assertFalse($p->can(getItemByTypeName('Printer', '_test_printer_ent2', true), READ)); }