Fixed undefined metod in unit/PermissionTest (#3422)

* Refactored AssetsTransformer

Casted all ids to int, escaped all text values,

* Added warranty_expires attribute to Asset model

$asset->warranty_expires contains a Carbon object with the warranty
expiration date. Returns null when either purchase_date or
warranty_months are not set.

* Ignoring php-cs cache files

* Restored asset tests expectations

Work in progress - tests still fail

* API controller refactoring, fixed HTTP status codes in responses

* Restored $request->get - debugging

* Added further checks in ApiAssetsCest::updateAssetWithPatch

* Fixed undefined method

* Fixed initial underscore trimmed by str_slug

* CustomFieldTest now works where intl PHP extension is not installed

If a server doesn't have the intl php extension installed, the custom fields tests failed. Now the tests perform the same check done in the CustomField class.
This commit is contained in:
Andrea Bergamasco 2017-03-14 16:39:03 +01:00 committed by snipe
parent e03ebc3fd0
commit 99cc8293ef
3 changed files with 86 additions and 71 deletions

View file

@ -32,14 +32,12 @@ class CustomField extends Model
public static function name_to_db_name($name)
{
return "_snipeit_".preg_replace("/[^a-zA-Z0-9]/", "_", strtolower($name));
return "_snipeit_" . preg_replace("/[^a-zA-Z0-9]/", "_", strtolower($name));
}
public static function boot()
{
self::created(function ($custom_field)
{
self::created(function ($custom_field) {
\Log::debug("\n\nCreating Original Name: ".$custom_field->name);
\Log::debug('Creating Column Name: '.$custom_field->convertUnicodeDbSlug());
@ -55,36 +53,30 @@ class CustomField extends Model
$custom_field->db_column = $custom_field->convertUnicodeDbSlug();
$custom_field->save();
});
self::updating(function ($custom_field)
{
self::updating(function ($custom_field) {
\Log::debug('Updating column name');
\Log::debug('Updating Original Name: '.$custom_field->getOriginal("name"));
\Log::debug('Updating New Column Name: '.$custom_field->convertUnicodeDbSlug());
if ($custom_field->isDirty("name")) {
if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug()))
{
if (Schema::hasColumn(CustomField::$table_name, $custom_field->convertUnicodeDbSlug())) {
\Log::debug('Column already exists. Nothing to update.');
return true;
}
\Log::debug('Updating column name to.'.$custom_field->convertUnicodeDbSlug());
return Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
$table->renameColumn($custom_field->convertUnicodeDbSlug($custom_field->getOriginal("name")), $custom_field->convertUnicodeDbSlug());
});
}
return true;
});
self::deleting(function ($custom_field)
{
self::deleting(function ($custom_field) {
return Schema::table(CustomField::$table_name, function ($table) use ($custom_field) {
$table->dropColumn($custom_field->convertUnicodeDbSlug());
});
@ -154,7 +146,6 @@ class CustomField extends Model
$result[$arr_parts[0]] = $arr_parts[0];
}
}
}
@ -176,16 +167,11 @@ class CustomField extends Model
$id = $this->id ? $this->id : 'xx';
if (!function_exists('transliterator_transliterate')) {
$long_slug = str_slug('_snipeit_'.\Patchwork\Utf8::utf8_encode(trim($name)),'_');
$long_slug = '_snipeit_' . str_slug(\Patchwork\Utf8::utf8_encode(trim($name)), '_');
} else {
$long_slug = '_snipeit_'.Utf8Slugger::slugify($name,'_');
$long_slug = '_snipeit_' . Utf8Slugger::slugify($name, '_');
}
return substr($long_slug, 0, 50).'_'.$id;
return substr($long_slug, 0, 50) . '_' . $id;
}
}

View file

@ -5,93 +5,124 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/*
* Test strings for db column names gathered from
* http://www.omniglot.com/language/phrases/hovercraft.htm
*/
class CustomFieldTest extends \Codeception\TestCase\Test
{
protected $tester;
use DatabaseMigrations;
protected $tester;
use DatabaseMigrations;
public function testConstructor() {
$customfield = new CustomField();
}
public function testConstructor()
{
$customfield = new CustomField();
}
public function testFormat() {
$customfield = factory(CustomField::class, 'customfield-ip')->make();
$values = [
'name' => $customfield->name,
'format' => $customfield->format,
'element' => $customfield->element,
];
public function testFormat()
{
$customfield = factory(CustomField::class, 'customfield-ip')->make();
$values = [
'name' => $customfield->name,
'format' => $customfield->format,
'element' => $customfield->element,
];
$this->assertEquals($customfield->getAttributes()['format'],CustomField::$PredefinedFormats['IP']); //this seems undocumented...
$this->assertEquals($customfield->format,"IP");
}
$this->assertEquals($customfield->getAttributes()['format'], CustomField::$PredefinedFormats['IP']); //this seems undocumented...
$this->assertEquals($customfield->format, "IP");
}
public function testDbNameAscii() {
$customfield = new CustomField();
$customfield->name="My hovercraft is full of eels";
$customfield->id = 1337;
$this->assertEquals($customfield->convertUnicodeDbSlug(),"_snipeit_my_hovercraft_is_full_of_eels_1337");
}
public function testDbNameAscii()
{
$customfield = new CustomField();
$customfield->name = "My hovercraft is full of eels";
$customfield->id = 1337;
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_my_hovercraft_is_full_of_eels_1337");
}
// Western Europe
public function testDbNameLatin() {
public function testDbNameLatin()
{
$customfield=new CustomField();
$customfield->name="My hovercraft is full of eels";
$customfield->id = 1337;
$this->assertEquals($customfield->convertUnicodeDbSlug(),"_snipeit_my_hovercraft_is_full_of_eels_1337");
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_my_hovercraft_is_full_of_eels_1337");
}
// Asian
public function testDbNameChinese() {
public function testDbNameChinese()
{
$customfield=new CustomField();
$customfield->name="我的氣墊船裝滿了鱔魚";
$customfield->id = 1337;
$this->assertEquals($customfield->convertUnicodeDbSlug(),"_snipeit_wo_de_qi_dian_chuan_zhuang_man_le_shan_yu_1337");
if (function_exists('transliterator_transliterate')) {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_wo_de_qi_dian_chuan_zhuang_man_le_shan_yu_1337");
} else {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_aecsae0ase1eaeaeoees_1337");
}
}
public function testDbNameJapanese() {
public function testDbNameJapanese()
{
$customfield=new CustomField();
$customfield->name="私のホバークラフトは鰻でいっぱいです";
$customfield->id = 1337;
$this->assertEquals($customfield->convertUnicodeDbSlug(),"_snipeit_sinohohakurafutoha_manteihhaitesu_1337");
if (function_exists('transliterator_transliterate')) {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_sinohohakurafutoha_manteihhaitesu_1337");
} else {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_caafafafaafcafafae0aaaaaaa_1337");
}
}
public function testDbNameKorean() {
$customfield=new CustomField();
$customfield->name="내 호버크라프트는 장어로 가득 차 있어요";
public function testDbNameKorean()
{
$customfield = new CustomField();
$customfield->name = "내 호버크라프트는 장어로 가득 차 있어요";
$customfield->id = 1337;
$this->assertEquals($customfield->convertUnicodeDbSlug(),"_snipeit_nae_hobeokeulapeuteuneun_jang_eolo_gadeug_1337");
if (function_exists('transliterator_transliterate')) {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_nae_hobeokeulapeuteuneun_jang_eolo_gadeug_1337");
} else {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_e_ie2ieiises_izieoe_e0e_i0_iziis_1337");
}
}
// Nordic languages
public function testDbNameNonLatinEuro() {
$customfield=new CustomField();
$customfield->name="Mój poduszkowiec jest pełen węgorzy";
public function testDbNameNonLatinEuro()
{
$customfield = new CustomField();
$customfield->name = "Mój poduszkowiec jest pełen węgorzy";
$customfield->id = 1337;
$this->assertEquals($customfield->convertUnicodeDbSlug(),"_snipeit_moj_poduszkowiec_jest_pelen_wegorzy_1337");
if (function_exists('transliterator_transliterate')) {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_moj_poduszkowiec_jest_pelen_wegorzy_1337");
} else {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_ma3j_poduszkowiec_jest_peaen_waegorzy_1337");
}
}
//
public function testDbNameTurkish() {
$customfield=new CustomField();
$customfield->name="Hoverkraftım yılan balığı dolu";
public function testDbNameTurkish()
{
$customfield = new CustomField();
$customfield->name = "Hoverkraftım yılan balığı dolu";
$customfield->id = 1337;
$this->assertEquals($customfield->convertUnicodeDbSlug(),"_snipeit_hoverkraftim_yilan_baligi_dolu_1337");
if (function_exists('transliterator_transliterate')) {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_hoverkraftim_yilan_baligi_dolu_1337");
} else {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_hoverkraftaem_yaelan_balaeaeyae_dolu_1337");
}
}
public function testDbNameArabic() {
public function testDbNameArabic()
{
$customfield=new CustomField();
$customfield->name="حَوّامتي مُمْتِلئة بِأَنْقَلَيْسون";
$customfield->id = 1337;
$this->assertEquals($customfield->convertUnicodeDbSlug(),"_snipeit_hwamty_mmtlyt_banqlyswn_1337");
if (function_exists('transliterator_transliterate')) {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_hwamty_mmtlyt_banqlyswn_1337");
} else {
$this->assertEquals($customfield->convertUnicodeDbSlug(), "_snipeit_ouzuuouoaus_uuuuoauuooc_ououzuuuuzuuzusuo_1337");
}
}
}

View file

@ -420,10 +420,8 @@ class PermissionsTest extends TestCase
$this->actingAs($user);
foreach ($routes as $route => $response) {
// dd($this->get(route($route)));
// echo($this->get(route($route))->dump());
$this->get($route)
->assertResponseStatus($response);
->assertStatus($response);
}
}
}