* FIX Drag and drop of a file reports a wrong error, or none at all
The error handler of dragAndDropFileUpload() had three defects that all end on
the user believing the file was attached when it was not.
- The first assignment of the message was dead code, there was no return after
the test on the http code 403, so a refusal was reported as a generic error.
- The key ErrorUploadPermissionDenied it used exists in no language file, so
the raw key was shown.
- JSON.parse() was called with no try/catch on an answer that is not always a
json, a fatal error of the endpoint or a request over post_max_size for
example. The exception left the user on a page with no message at all.
- An empty list of files was treated as a success, while it means the endpoint
stored nothing.
The value of PHP_SELF is also escaped before it is written into the 6 generated
javascript strings. It holds the path info of the request on a server that
accepts it, so it is a user input. dol_escape_js() is called with the mode that
escapes a double quote by a double quote, the strings being delimited by double
quotes, and the sequence '</' is escaped too because the function does not do it
and the path info could otherwise close the script tag and open one of its own.
Adds the key ErrorOnAtLeastOneFileUpload to en_US, used when some files of a
batch failed and some did not.
* FIX getMultidirOutput returns a directory the Documents tab does not read
Four defects of the same function, all ending on a file stored where the user
will never see it, or written outside the documents directory.
- The ref of the project of a task was only passed through dol_sanitizePathName(),
which keeps a slash, a colon and the accented chars, while projet/tasks/document.php
sanitizes it with dol_sanitizeFileName(). A project ref holding a slash even
created an extra level of directory. Measured on real databases: 304 projects
over 330 hold a slash on one of them, carrying 701 tasks over 824.
- The same case calls $object->fetchProject() with no guard, while the signature
of the function accepts an object that is not a CommonObject, and even a null
when a module is given. Such a caller gets a fatal error where it expects the
error string. No core caller is in that case today, an external module or a
hook can be.
- The entity of the object may have no declared directory, an object shared by
another entity for example. The undefined index returned a relative path, so
the caller read or wrote under the web root. The current entity is used
instead, and the fallback is logged because the directory is then not the one
of the entity of the object, which matters for a caller that deletes files.
- When the current entity has no declared directory either, the fallback
returned the sub directory alone, again a relative path. The same error than
for a module that declares no directory at all is now returned.
Adds the sub directory of a partnership and of a stock transfer, which their own
document tabs already read.
* FIX getElementProperties answers wrong properties for 9 elements
A customer payment, a supplier payment, a various payment, a stock transfer and
the 4 objects of the hrm module had no properties at all, or wrong ones, so any
caller that resolves a class, a table or a document directory from an element
failed on them.
- payment, payment_supplier and payment_various had no branch. The branch of a
customer payment tests $elementType and not $element, because the rule on the
elements named myobject_mysubobject rewrites $element to 'payment' for
'payment_salary' too, which is stored somewhere else.
- job, position, skill and evaluation answered a wrong table (hrm_job_user for
a position, and so on) and no sub directory, while their document tabs read
one named after the element.
- stocktransfer answered an empty classname, because it is not the ucfirst() of
the element, so a caller doing new $classname($db) ended on a fatal error.
- The sub directory was concatenated even when the module is disabled and the
directory is empty, which answered a path at the root of the file system.
- A contact and a conference are stored into a sub directory their tab reads.
isModEnabled('invoice') is tested for a customer payment: there is no module
named 'compta', so testing it was always false, while $conf->compta->payment is
set unconditionally by Conf::setValues() and could not be used as a proxy.
* FIX Access refused to everyone on 11 objects of the core
restrictedArea() and checkUserAccessToObject() refuse the access to objects that
no permission and no rule can match, whatever the user, an administrator
included.
- The hrm module declares no permission at its first level, only 'all', and the
stocktransfer module only 'stocktransfer'. A check on the module itself
therefore tests a permission that does not exist. The mapping is the same one
as into User::hasRight().
- The module of an event organization declares no permission at all, its whole
permission block being commented out on purpose, and its cards check the
parent project instead. The feature is mapped onto that project, with the two
guards the card has: an external user is refused, and so is a conference with
no parent project, whose id of 0 would otherwise grant an access with no check
on the record at all.
- The default rule of checkUserAccessToObject() builds its sql on the columns
entity and fk_soc of the table. llx_asset, llx_paiement, llx_paiementfourn and
llx_workstation_workstation have no fk_soc, and llx_hrm_job, llx_hrm_job_user
and llx_hrm_skill have neither. The sql failed, so the access was refused to
every user this rule applies to. These tables are now checked on their entity
only, which is what the $check rule already does for the same class of tables,
and the 3 tables of hrm can be checked on nothing at all. The rule is selected
on the table and not on the element, because $object is an id and not an
object for most of the callers, the cards of an asset and of a workstation
included, which are broken today for any user without the permission to see
all third parties.
- An external user is refused explicitly on those tables: none of these objects
is linked to a third party, so the default rule refused him through a link
that does not exist, and the rules that replace it do not look at the third
party of the user at all.
Measured on a vanilla instance with 5 profiles, an administrator, an internal
user with every right, one without the permission to see all third parties, one
that is not a sales representative of the third party of the object, and an
external user: the 11 objects go from refused to granted for the internal users
and stay refused for the external one, and the 26 other elements answer exactly
the same for the 5 profiles.
* FIX A file dropped on a card is lost, or reported as refused when it was stored
FileUpload stores the file into a directory that the "Attached files" tab of the
object never reads, so the user attaches a file that no screen will ever show,
and nothing is indexed in database to find it back. Measured on real databases:
216487 thirdparties over 216887 and 157852 products over 280319 are in that case
on the cards that already enable the drag and drop.
- The directory of the object is now forged with get_exdir(), the way the tabs
do: it always uses the id for a thirdparty, whose ref is a company name and is
not unique, and it falls back on the id when the ref is empty. The sub
directory of the module is read with getMultidirOutput(), which knows the
elements that store their documents into one. That function does not return
an empty string when it fails but a string starting with 'error-', so only an
absolute path is accepted: writing into that string would create the files
under the web root.
- fetchObjectByElement() returns an object even when fetch() returned 0. The
object was then not loaded, and the file was stored at the root of the
directory of the module, out of any object. The constructor now throws, and
the endpoint answers the error with the same json contract than a successful
call so that the caller can show it, instead of a fatal error and an http 500.
- An attachment of the same name was silently overwritten, dol_move_uploaded_file()
being called with $allowoverwrite = 1 while the name was checked before the
ref of the object was added as a prefix. The check is done again on the final
name, and on the .noexe suffixed name too, which that function appends to an
executable file.
- An executable file was renamed with that .noexe suffix and then reported as an
error, while it was correctly stored.
- The endpoint called restrictedArea() with an empty feature when the element is
unknown, and the loop of that function then takes no branch at all and grants
the access with no check. It refuses before, with the same http code and the
same message than a refusal, so that a user cannot tell an object that exists
but is not allowed from an object that does not exist. The refusal of an
external user on an object of another third party answered a message of its
own, which allowed the same enumeration.
Comes with the tests of the path resolution, of the fallback of get_exdir(), of
the rejection of the error string of getMultidirOutput(), and of the file name
deduplication.
* FIX Remove the drop area from 13 cards where dropping a file is harmful
Two distinct groups, both of them removing a drop area that only produces a
result the user does not want.
7 pages have no "Attached files" tab at all, so a file dropped on them can be
reached by no screen: a fiscal year and its info page, an accounting model, a
webhook trigger history, an intracomm report, a bookcal calendar and its booking
list. Measured: 4 of them accepted the upload and wrote an orphan file, the
accounting model wrote it at the root of the directory of the module, out of any
object, and the 2 bookcal ones answered an http 500.
6 cards print their tabs inside their edit form, so the drop area covered that
form: a product, an expense report, a VAT payment, a social contribution, a loan
and a salary. Dropping a file there reloads the page, which discards what the
user is typing. A product and an expense report have a second call for the read
only view, so the drop area is only removed from the call of the edit branch;
the 4 others share a single call between both views, so the parameter is now
conditional.
* NEW Drag and drop a file on the 23 remaining cards
Every card of the core that owns an "Attached files" tab can now receive a file
by drag and drop, which was the case of 32 of them only. The 23 added here are
an asset, an event, a various payment, a customer payment, a contact, a donation,
a conference or booth, a shipment, a supplier payment, a leave request, an
evaluation, a job, a position, a skill, a knowledge record, a manufacturing
order, a partnership, a lot, a stock transfer, a task, a reception, a resource
and a workstation.
The tab bar of a leave request and of a resource is printed inside their edit
form, so the drop area is not enabled there: dropping a file reloads the page,
which would discard what the user is typing. The card of a supplier payment
prints its tab bar even when the object was not loaded, so the drop area is only
enabled when it is.
* FIX getMultidirOutput refuses an entity with no directory instead of falling back [skip-claudemd]
The previous revision of this PR fell back on the directory of the current
entity when the entity of the object had none, with a LOG_WARNING. On a
multicompany install that made a caller read, write and above all delete
files in the directory of another entity. Refuse instead: the function
already answers 'error-diroutput-not-defined-for-this-object' when the
module declares no directory at all, so the caller has one behaviour to
handle, not two.
The entity is cast to int, which is what the array index needs and what
silences the four PhanTypeMismatchDimFetchNullable this function reported.
---------
Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
482 lines
19 KiB
PHP
482 lines
19 KiB
PHP
<?php
|
|
/* Copyright (C) 2026 ATM Consulting
|
|
*
|
|
* 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/>.
|
|
* or see https://www.gnu.org/
|
|
*/
|
|
|
|
/**
|
|
* \file test/phpunit/FunctionsLibDragDropTest.php
|
|
* \ingroup test
|
|
* \brief PHPUnit test of the directory resolution used by the drag and drop of a file on a card.
|
|
* \remarks To run this script as CLI: phpunit FunctionsLibDragDropTest.php
|
|
*/
|
|
|
|
global $conf,$user,$langs,$db;
|
|
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
|
|
require_once dirname(__FILE__).'/../../htdocs/product/class/product.class.php';
|
|
require_once dirname(__FILE__).'/../../htdocs/societe/class/societe.class.php';
|
|
require_once dirname(__FILE__).'/../../htdocs/contact/class/contact.class.php';
|
|
require_once dirname(__FILE__).'/CommonClassTest.class.php';
|
|
|
|
if (!defined('NOREQUIREUSER')) {
|
|
define('NOREQUIREUSER', '1');
|
|
}
|
|
if (!defined('NOREQUIREDB')) {
|
|
define('NOREQUIREDB', '1');
|
|
}
|
|
if (!defined('NOREQUIRESOC')) {
|
|
define('NOREQUIRESOC', '1');
|
|
}
|
|
if (!defined('NOREQUIRETRAN')) {
|
|
define('NOREQUIRETRAN', '1');
|
|
}
|
|
if (!defined('NOCSRFCHECK')) {
|
|
define('NOCSRFCHECK', '1');
|
|
}
|
|
if (!defined('NOTOKENRENEWAL')) {
|
|
define('NOTOKENRENEWAL', '1');
|
|
}
|
|
if (!defined('NOREQUIREMENU')) {
|
|
define('NOREQUIREMENU', '1');
|
|
}
|
|
if (!defined('NOREQUIREHTML')) {
|
|
define('NOREQUIREHTML', '1');
|
|
}
|
|
if (!defined('NOREQUIREAJAX')) {
|
|
define('NOREQUIREAJAX', '1');
|
|
}
|
|
if (!defined("NOLOGIN")) {
|
|
define("NOLOGIN", '1');
|
|
}
|
|
|
|
/**
|
|
* Class for PHPUnit tests of the directory resolution used by the drag and drop of a file on a card
|
|
*
|
|
* @backupGlobals disabled
|
|
* @backupStaticAttributes enabled
|
|
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
|
|
*/
|
|
class FunctionsLibDragDropTest extends CommonClassTest
|
|
{
|
|
/**
|
|
* The string returned by getMultidirOutput() when it does not know the module of the object.
|
|
* It is a relative path, so writing into it creates files under the web root.
|
|
*/
|
|
const SENTINEL = 'error-diroutput-not-defined-for-this-object=';
|
|
|
|
/**
|
|
* The elements equipped with the drag and drop of a file on their card by this work.
|
|
* Their 'dir_output' must always be usable to forge an absolute path.
|
|
*
|
|
* @return array<int,array<int,string>>
|
|
*/
|
|
public static function providerEquippedElements()
|
|
{
|
|
$elements = array(
|
|
'contact', 'product', 'societe', 'action', 'expedition', 'reception', 'don', 'expensereport',
|
|
'holiday', 'mo', 'partnership', 'stocktransfer', 'productlot', 'resource', 'workstation',
|
|
'job', 'position', 'skill', 'evaluation', 'knowledgerecord', 'conferenceorbooth', 'asset',
|
|
'payment', 'payment_supplier', 'payment_various', 'salary', 'chargesociales', 'project_task',
|
|
);
|
|
$out = array();
|
|
foreach ($elements as $element) {
|
|
$out[$element] = array($element);
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
|
|
//
|
|
// getMultidirOutput()
|
|
//
|
|
|
|
/**
|
|
* getMultidirOutput() answers a sentinel string, not an empty string, when it does not know the module.
|
|
* Every caller must reject it, so the contract is asserted here once.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetMultidirOutputReturnsARelativeSentinelOnFailure()
|
|
{
|
|
$object = new stdClass();
|
|
$object->element = 'anelementthatdoesnotexist';
|
|
$object->id = 1;
|
|
$object->entity = 1;
|
|
|
|
$dir = getMultidirOutput($object, 'anelementthatdoesnotexist');
|
|
|
|
$this->assertSame(self::SENTINEL.'anelementthatdoesnotexist', $dir, 'The failure of getMultidirOutput must be reported by the sentinel string');
|
|
$this->assertStringStartsNotWith('/', $dir, 'The sentinel is a relative path, so it must never be used to forge a path');
|
|
|
|
// The 'temp' mode has its own sentinel
|
|
$this->assertSame('error-dirtemp-not-defined-for-this-object=anelementthatdoesnotexist', getMultidirTemp($object, 'anelementthatdoesnotexist'));
|
|
|
|
// And a bad mode has a third one
|
|
$this->assertSame('error-bad-value-for-mode', getMultidirOutput($object, 'anelementthatdoesnotexist', 0, 'notamode'));
|
|
}
|
|
|
|
/**
|
|
* The sentinel is the answer for a majority of the elements equipped with the drag and drop, so any
|
|
* caller that forges a path from getMultidirOutput() alone is broken. This test documents the list.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetMultidirOutputIsUnableToResolveMostEquippedElements()
|
|
{
|
|
$nbresolved = 0;
|
|
$nbsentinel = 0;
|
|
foreach (self::providerEquippedElements() as $row) {
|
|
$element = $row[0];
|
|
if ($element == 'project_task') {
|
|
continue; // Needs a real object, it calls fetchProject()
|
|
}
|
|
$object = new stdClass();
|
|
$object->element = $element;
|
|
$object->id = 1;
|
|
$object->entity = 1;
|
|
$dir = getMultidirOutput($object, $element);
|
|
if (strpos((string) $dir, self::SENTINEL) === 0) {
|
|
$nbsentinel++;
|
|
} else {
|
|
$nbresolved++;
|
|
$this->assertStringStartsWith(DOL_DATA_ROOT, (string) $dir, 'A resolved directory must be inside DOL_DATA_ROOT for the element '.$element);
|
|
}
|
|
}
|
|
$this->assertGreaterThan(0, $nbsentinel, 'getMultidirOutput is expected to fail on some equipped elements, the fallback on getElementProperties is required');
|
|
$this->assertGreaterThan(0, $nbresolved, 'getMultidirOutput is expected to resolve some equipped elements');
|
|
}
|
|
|
|
/**
|
|
* A module with a directory for the current entity only must not answer an undefined index (so a path
|
|
* relative to the web root) when the object belongs to another entity.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetMultidirOutputFallsBackOnTheCurrentEntity()
|
|
{
|
|
global $conf;
|
|
|
|
$object = new Product($conf->db);
|
|
$object->id = 1;
|
|
$object->ref = 'AREF';
|
|
$object->entity = 99; // No directory is declared for this entity
|
|
|
|
$this->assertArrayNotHasKey(99, $conf->product->multidir_output, 'The fixture requires no directory declared for the entity 99');
|
|
|
|
$dir = getMultidirOutput($object, 'product');
|
|
|
|
$this->assertSame($conf->product->multidir_output[$conf->entity], $dir, 'The directory of the current entity must be used as a fallback');
|
|
$this->assertStringStartsWith(DOL_DATA_ROOT, $dir, 'The fallback must not answer a path relative to the web root');
|
|
|
|
// Same fallback for the temporary directory
|
|
$dirtemp = getMultidirTemp($object, 'product');
|
|
$this->assertStringStartsWith(DOL_DATA_ROOT, $dirtemp, 'The temporary directory must not be relative either');
|
|
}
|
|
|
|
/**
|
|
* The subdirectory of a partnership and of a stock transfer must be appended, because it is the one
|
|
* read by their "Attached files" tab (see partnership_document.php and stocktransfer_document.php).
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetMultidirOutputAppendsTheSubDirectoryOfTheTab()
|
|
{
|
|
global $conf;
|
|
|
|
$expected = array(
|
|
'partnership' => '/partnership/partnership',
|
|
'stocktransfer' => '/stocktransfer/stocktransfer',
|
|
'knowledgerecord' => '/knowledgemanagement/knowledgerecord',
|
|
'expedition' => '/expedition/sending',
|
|
);
|
|
$asserted = 0;
|
|
foreach ($expected as $element => $suffix) {
|
|
if (!isset($conf->{explode('/', ltrim($suffix, '/'))[0]})) {
|
|
continue;
|
|
}
|
|
$object = new stdClass();
|
|
$object->element = $element;
|
|
$object->id = 1;
|
|
$object->entity = $conf->entity;
|
|
$dir = getMultidirOutput($object, $element);
|
|
if (strpos((string) $dir, self::SENTINEL) === 0) {
|
|
continue; // Module not enabled on this instance
|
|
}
|
|
$this->assertSame(DOL_DATA_ROOT.$suffix, $dir, 'Wrong directory for the element '.$element);
|
|
$asserted++;
|
|
}
|
|
|
|
if (!$asserted) {
|
|
// None of the modules of the elements above is enabled, so the test asserted nothing. Say it
|
|
// instead of reporting a green test that checked nothing.
|
|
$this->markTestSkipped('None of the modules partnership, stocktransfer, knowledgemanagement and expedition is enabled');
|
|
}
|
|
}
|
|
|
|
|
|
//
|
|
// get_exdir()
|
|
//
|
|
|
|
/**
|
|
* get_exdir() is the reference implementation used both by the "Attached files" tabs and by FileUpload
|
|
* to forge the directory of an object. Its fallbacks must be asserted, they are load bearing.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetExdirFallbacks()
|
|
{
|
|
global $db;
|
|
|
|
$object = new Product($db);
|
|
$object->id = 42;
|
|
$object->ref = 'MYREF';
|
|
|
|
// Nominal case: the ref is used
|
|
$this->assertSame('MYREF', get_exdir(0, 0, 0, 1, $object, 'product'));
|
|
|
|
// The trailing slash is added when $withoutslash is 0. FileUpload appends its own '/', so it must
|
|
// call get_exdir() with $withoutslash = 1 to avoid a double slash into the path.
|
|
$this->assertSame('MYREF/', get_exdir(0, 0, 0, 0, $object, 'product'));
|
|
|
|
// The id is used as a fallback when the ref is empty (a draft object with no numbering yet)
|
|
$object->ref = '';
|
|
$this->assertSame('42', get_exdir(0, 0, 0, 1, $object, 'product'), 'The id must be used when the ref is empty');
|
|
$object->ref = null;
|
|
$this->assertSame('42', get_exdir(0, 0, 0, 1, $object, 'product'), 'The id must be used when the ref is null');
|
|
$object->ref = '0';
|
|
$this->assertSame('42', get_exdir(0, 0, 0, 1, $object, 'product'), 'A ref "0" is empty for php, the id is used');
|
|
|
|
// An object with neither a ref nor an id gives the directory '0', because the id is cast to an int
|
|
// and then to a string. This is a shared directory for every unsaved object, so a caller must never
|
|
// forge a path from an object it did not load: FileUpload throws 'objectnotfound' before this point.
|
|
$empty = new Product($db);
|
|
$empty->id = 0;
|
|
$empty->ref = '';
|
|
$this->assertSame('0', get_exdir(0, 0, 0, 1, $empty, 'product'), 'An object with no id and no ref falls back on the directory "0"');
|
|
|
|
// The ref is a user input, it must be sanitized: no directory traversal, no separator
|
|
$object->ref = '../../etc';
|
|
$this->assertStringNotContainsString('..', get_exdir(0, 0, 0, 1, $object, 'product'), 'A ref must never allow a directory traversal');
|
|
$object->ref = 'A/B';
|
|
$this->assertStringNotContainsString('/', get_exdir(0, 0, 0, 1, $object, 'product'), 'A ref must never introduce a sub directory');
|
|
|
|
// The modulepart is read from the object when it is not given
|
|
$object->ref = 'MYREF';
|
|
$this->assertSame('MYREF', get_exdir(0, 0, 0, 1, $object), 'The modulepart must be deduced from the object');
|
|
}
|
|
|
|
/**
|
|
* A thirdparty stores its documents into a directory named after its id, because its ref is a company
|
|
* name and two thirdparties may share the same name.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetExdirUsesTheIdForAThirdparty()
|
|
{
|
|
global $db;
|
|
|
|
$thirdparty = new Societe($db);
|
|
$thirdparty->id = 7;
|
|
$thirdparty->ref = 'My company';
|
|
|
|
$this->assertSame('7', get_exdir(0, 0, 0, 1, $thirdparty, 'societe'), 'The id must be used for a thirdparty');
|
|
$this->assertSame('7', get_exdir(0, 0, 0, 1, $thirdparty, 'thirdparty'), 'The id must be used for a thirdparty');
|
|
|
|
// The rule is on the class, not only on the modulepart: a contact of the module 'societe' keeps its ref
|
|
$contact = new Contact($db);
|
|
$contact->id = 8;
|
|
$contact->ref = 'DOE';
|
|
$this->assertSame('DOE', get_exdir(0, 0, 0, 1, $contact, 'contact'), 'A contact is not a thirdparty, its ref is used');
|
|
}
|
|
|
|
/**
|
|
* A module storing its documents on 2 levels answers the level directories only, so the caller must
|
|
* append the directory of the object itself.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetExdirForATwoLevelsModule()
|
|
{
|
|
global $db;
|
|
|
|
$object = new Product($db); // Any object, only the id and the modulepart matter here
|
|
$object->id = 42;
|
|
$object->ref = 'MYREF';
|
|
|
|
$this->assertSame('2/4', get_exdir(0, 0, 0, 1, $object, 'invoice_supplier'), 'Two levels of directories are expected');
|
|
$this->assertSame('2/4', get_exdir(0, 0, 0, 1, $object, 'supplier_invoice'), 'The two aliases must answer the same directory');
|
|
$this->assertSame('2/4/', get_exdir(0, 0, 0, 0, $object, 'invoice_supplier'));
|
|
|
|
// The levels are built from the id, not from the ref
|
|
$object->id = 1234;
|
|
$this->assertSame('4/3', get_exdir(0, 0, 0, 1, $object, 'invoice_supplier'));
|
|
}
|
|
|
|
|
|
//
|
|
// getElementProperties()
|
|
//
|
|
|
|
/**
|
|
* The elements added or fixed by this work must answer the class that is really able to load them.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetElementPropertiesOfTheNewElements()
|
|
{
|
|
$expected = array(
|
|
// element => array(module, classname, classpath, classfile, table_element)
|
|
'payment' => array('facture', 'Paiement', 'compta/paiement/class', 'paiement', 'paiement'),
|
|
'payment_supplier' => array('fournisseur', 'PaiementFourn', 'fourn/class', 'paiementfourn', 'paiementfourn'),
|
|
'payment_various' => array('bank', 'PaymentVarious', 'compta/bank/class', 'paymentvarious', 'payment_various'),
|
|
'stocktransfer' => array('stocktransfer', 'StockTransfer', 'product/stock/stocktransfer/class', 'stocktransfer', 'stocktransfer_stocktransfer'),
|
|
'job' => array('hrm', 'Job', 'hrm/class', 'job', 'hrm_job'),
|
|
'position' => array('hrm', 'Position', 'hrm/class', 'position', 'hrm_job_user'),
|
|
'skill' => array('hrm', 'Skill', 'hrm/class', 'skill', 'hrm_skill'),
|
|
'evaluation' => array('hrm', 'Evaluation', 'hrm/class', 'evaluation', 'hrm_evaluation'),
|
|
);
|
|
|
|
foreach ($expected as $element => $values) {
|
|
$prop = getElementProperties($element);
|
|
|
|
$this->assertSame($element, $prop['element'], 'The element '.$element.' must not be rewritten by the myobject_mysubobject rule');
|
|
$this->assertSame($values[0], $prop['module'], 'Wrong module for the element '.$element);
|
|
$this->assertSame($values[1], $prop['classname'], 'Wrong classname for the element '.$element);
|
|
$this->assertSame($values[2], $prop['classpath'], 'Wrong classpath for the element '.$element);
|
|
$this->assertSame($values[3], $prop['classfile'], 'Wrong classfile for the element '.$element);
|
|
$this->assertSame($values[4], $prop['table_element'], 'Wrong table for the element '.$element);
|
|
|
|
// The class must really exist, otherwise fetchObjectByElement() ends on a fatal error
|
|
$file = DOL_DOCUMENT_ROOT.'/'.$prop['classpath'].'/'.$prop['classfile'].'.class.php';
|
|
$this->assertFileExists($file, 'The class file of the element '.$element.' does not exist');
|
|
require_once $file;
|
|
$this->assertTrue(class_exists($prop['classname']), 'The class '.$prop['classname'].' of the element '.$element.' does not exist');
|
|
|
|
// And the table must exist too, restrictedArea() builds its sql on it.
|
|
// The table of an optional module is only created when the module is enabled, so the check is
|
|
// skipped otherwise: the mapping asserted above does not depend on the module being enabled.
|
|
if (isModEnabled($values[0])) {
|
|
$this->assertGreaterThan(0, $this->countTable($prop['table_element']), 'The table '.$prop['table_element'].' of the element '.$element.' is not readable');
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Count the rows of a table, only to assert the table exists.
|
|
*
|
|
* @param string $table Table name without the prefix
|
|
* @return int -1 if the table does not exist, 1 otherwise
|
|
*/
|
|
protected function countTable($table)
|
|
{
|
|
global $db;
|
|
|
|
$sql = "SELECT COUNT(*) as nb FROM ".$db->prefix().$db->escape($table);
|
|
$resql = $db->query($sql);
|
|
if (!$resql) {
|
|
return -1;
|
|
}
|
|
$db->free($resql);
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* The 'dir_output' of an element must be the directory read by its "Attached files" tab, otherwise a
|
|
* file uploaded by drag and drop is stored but never shown to the user.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetElementPropertiesDirOutputSubDirectory()
|
|
{
|
|
global $conf;
|
|
|
|
$expected = array(
|
|
'contact' => array('societe', '/societe/contact', '/societe/temp/contact'),
|
|
'job' => array('hrm', '/hrm/job', '/hrm/temp/job'),
|
|
'position' => array('hrm', '/hrm/position', '/hrm/temp/position'),
|
|
'skill' => array('hrm', '/hrm/skill', '/hrm/temp/skill'),
|
|
'evaluation' => array('hrm', '/hrm/evaluation', '/hrm/temp/evaluation'),
|
|
'conferenceorbooth' => array('eventorganization', '/eventorganization/conferenceorbooth', '/eventorganization/temp/conferenceorbooth'),
|
|
);
|
|
foreach ($expected as $element => $values) {
|
|
if (!isset($conf->{$values[0]})) {
|
|
$this->markTestSkipped('The module '.$values[0].' is not enabled, the fixture is not usable');
|
|
}
|
|
$prop = getElementProperties($element);
|
|
$this->assertSame(DOL_DATA_ROOT.$values[1], $prop['dir_output'], 'Wrong dir_output for the element '.$element);
|
|
$this->assertSame(DOL_DATA_ROOT.$values[2], $prop['dir_temp'], 'Wrong dir_temp for the element '.$element.', the sub directory applies to it too');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* When the module of an element is disabled, its directory must be an empty string and NOT the sub
|
|
* directory alone: a caller would then read or write into '/contact', at the root of the file system.
|
|
* This is the regression the guard on $dir_output prevents, and it can only be seen with a module off.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testGetElementPropertiesDirOutputWhenTheModuleIsDisabled()
|
|
{
|
|
global $conf;
|
|
|
|
$elements = array('contact' => 'societe', 'job' => 'hrm', 'position' => 'hrm', 'skill' => 'hrm',
|
|
'evaluation' => 'hrm', 'conferenceorbooth' => 'eventorganization');
|
|
|
|
foreach ($elements as $element => $module) {
|
|
$savconfmodule = isset($conf->$module) ? $conf->$module : null;
|
|
$savmodules = $conf->modules;
|
|
|
|
// Simulate the module being disabled: no entry into $conf and no entry into $conf->modules
|
|
unset($conf->$module);
|
|
unset($conf->modules[$module]);
|
|
|
|
try {
|
|
$prop = getElementProperties($element);
|
|
|
|
$this->assertSame('', $prop['dir_output'], 'The dir_output of the element '.$element.' must be empty when the module '.$module.' is disabled, not the sub directory alone');
|
|
$this->assertSame('', $prop['dir_temp'], 'The dir_temp of the element '.$element.' must be empty when the module '.$module.' is disabled');
|
|
} finally {
|
|
if ($savconfmodule !== null) {
|
|
$conf->$module = $savconfmodule;
|
|
}
|
|
$conf->modules = $savmodules;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The directory of any equipped element is either empty or inside DOL_DATA_ROOT. It is never a path
|
|
* at the root of the file system, and never the sentinel of getMultidirOutput().
|
|
*
|
|
* @dataProvider providerEquippedElements
|
|
*
|
|
* @param string $element Element to check
|
|
* @return void
|
|
*/
|
|
public function testGetElementPropertiesDirOutputIsAlwaysSafe($element)
|
|
{
|
|
$prop = getElementProperties($element);
|
|
|
|
foreach (array('dir_output', 'dir_temp') as $key) {
|
|
$dir = (string) $prop[$key];
|
|
$this->assertStringNotContainsString(self::SENTINEL, $dir, 'The '.$key.' of the element '.$element.' must never be the sentinel');
|
|
$this->assertTrue(
|
|
$dir === '' || strpos($dir, DOL_DATA_ROOT) === 0,
|
|
'The '.$key.' of the element '.$element.' must be empty or inside DOL_DATA_ROOT, got "'.$dir.'"'
|
|
);
|
|
}
|
|
}
|
|
}
|