Skip to content

Fix viewing unattached domain records #19545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The present file will list all changes made to the project; according to the

### Changed
- Only unsolved/unclosed tickets will be shown in the dropdown when performing the "Merge as Followup" action.
- Domain records must be attached to a domain. Existing unattached records will remain but will require a domain if edited.

### Deprecated

Expand Down
78 changes: 78 additions & 0 deletions phpunit/functional/DomainRecordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace tests\units;

use DbTestCase;
use DomainRecord;

/* Test for inc/software.class.php */

class DomainRecordTest extends DbTestCase
{
public function testCanViewUnattached()
{
/** @var \DBmysql $DB */
global $DB;
$this->login();
$record = new DomainRecord();
// Unattached records are not allowed to be created anymore but may still exist in the DB. For the test, we need to directly add one to the DB.
$DB->insert(
'glpi_domainrecords',
[
'name' => __FUNCTION__,
'entities_id' => $this->getTestRootEntity(true),
'ttl' => 3600,
]
);
$this->assertTrue($record->getFromDB($DB->insertId()));

$this->assertTrue($record->canViewItem());
}

public function testPrepareInput()
{
$this->login();
$record = new DomainRecord();
$this->assertFalse($record->prepareInputForAdd([]));
$this->hasSessionMessages(ERROR, ['A domain is required']);

$created_record = $this->createItem('DomainRecord', ['domains_id' => 1]);
$this->assertEmpty($record->prepareInputForUpdate([]));
$this->assertFalse($record->prepareInputForUpdate(['domains_id' => 0]));
$this->hasSessionMessages(ERROR, ['A domain is required']);
$this->assertFalse($record->prepareInputForUpdate(['domains_id' => '']));
$this->hasSessionMessages(ERROR, ['A domain is required']);
}
}
10 changes: 10 additions & 0 deletions src/DomainRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DomainRecord extends CommonDBChild
public static $itemtype = 'Domain';
public static $items_id = 'domains_id';
public $dohistory = true;
public static $mustBeAttached = false;

public static function getTypeName($nb = 0)
{
Expand Down Expand Up @@ -258,6 +259,14 @@ public function defineTabs($options = [])
*/
private function prepareInput($input, $add = false)
{
if (($add && empty($input['domains_id'])) || (isset($input['domains_id']) && empty($input['domains_id']))) {
Session::addMessageAfterRedirect(
__('A domain is required'),
true,
ERROR
);
return false;
}

if ($add) {
if (isset($input['date_creation']) && empty($input['date_creation'])) {
Expand Down Expand Up @@ -361,6 +370,7 @@ public function showForm($ID, array $options = [])
'value' => $this->fields["domains_id"],
'entity' => $this->fields["entities_id"],
'rand' => $rand,
'display_emptychoice' => false,
]
);
}
Expand Down