ajout app

This commit is contained in:
2024-04-17 20:22:30 +02:00
parent cc017cfc5e
commit f9d05a2fd3
8025 changed files with 729805 additions and 0 deletions

View File

@ -0,0 +1,57 @@
<?php
namespace Tests\Unit;
use App\Models\Accessory;
use App\Models\Manufacturer;
use App\Models\Location;
use App\Models\Category;
use App\Models\Company;
use Tests\TestCase;
class AccessoryTest extends TestCase
{
public function testAnAccessoryBelongsToACompany()
{
$accessory = Accessory::factory()
->create(
[
'company_id' =>
Company::factory()->create()->id]);
$this->assertInstanceOf(Company::class, $accessory->company);
}
public function testAnAccessoryHasALocation()
{
$accessory = Accessory::factory()
->create(
[
'location_id' => Location::factory()->create()->id
]);
$this->assertInstanceOf(Location::class, $accessory->location);
}
public function testAnAccessoryBelongsToACategory()
{
$accessory = Accessory::factory()->appleBtKeyboard()
->create(
[
'category_id' =>
Category::factory()->create(
[
'category_type' => 'accessory'
]
)->id]);
$this->assertInstanceOf(Category::class, $accessory->category);
$this->assertEquals('accessory', $accessory->category->category_type);
}
public function testAnAccessoryHasAManufacturer()
{
$accessory = Accessory::factory()->appleBtKeyboard()->create(
[
'category_id' => Category::factory()->create(),
'manufacturer_id' => Manufacturer::factory()->apple()->create()
]);
$this->assertInstanceOf(Manufacturer::class, $accessory->manufacturer);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace Tests\Unit;
use App\Models\AssetMaintenance;
use Tests\TestCase;
class AssetMaintenanceTest extends TestCase
{
public function testZerosOutWarrantyIfBlank()
{
$c = new AssetMaintenance;
$c->is_warranty = '';
$this->assertTrue($c->is_warranty === 0);
$c->is_warranty = '4';
$this->assertTrue($c->is_warranty == 4);
}
public function testSetsCostsAppropriately()
{
$c = new AssetMaintenance();
$c->cost = '0.00';
$this->assertTrue($c->cost === null);
$c->cost = '9.54';
$this->assertTrue($c->cost === 9.54);
$c->cost = '9.50';
$this->assertTrue($c->cost === 9.5);
}
public function testNullsOutNotesIfBlank()
{
$c = new AssetMaintenance;
$c->notes = '';
$this->assertTrue($c->notes === null);
$c->notes = 'This is a long note';
$this->assertTrue($c->notes === 'This is a long note');
}
public function testNullsOutCompletionDateIfBlankOrInvalid()
{
$c = new AssetMaintenance;
$c->completion_date = '';
$this->assertTrue($c->completion_date === null);
$c->completion_date = '0000-00-00';
$this->assertTrue($c->completion_date === null);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace Tests\Unit;
use App\Models\Asset;
use App\Models\Category;
use App\Models\AssetModel;
use Tests\TestCase;
class AssetModelTest extends TestCase
{
public function testAnAssetModelContainsAssets()
{
$category = Category::factory()->create([
'category_type' => 'asset'
]);
$model = AssetModel::factory()->create([
'category_id' => $category->id,
]);
$asset = Asset::factory()->create([
'model_id' => $model->id
]);
$this->assertEquals(1, $model->assets()->count());
}
}

View File

@ -0,0 +1,159 @@
<?php
namespace Tests\Unit;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Category;
use Carbon\Carbon;
use Tests\TestCase;
class AssetTest extends TestCase
{
public function testAutoIncrement()
{
$this->settings->enableAutoIncrement();
$a = Asset::factory()->create(['asset_tag' => Asset::autoincrement_asset() ]);
$b = Asset::factory()->create(['asset_tag' => Asset::autoincrement_asset() ]);
$this->assertModelExists($a);
$this->assertModelExists($b);
}
public function testAutoIncrementCollision()
{
$this->settings->enableAutoIncrement();
// we have to do this by hand to 'simulate' two web pages being open at the same time
$a = Asset::factory()->make(['asset_tag' => Asset::autoincrement_asset() ]);
$b = Asset::factory()->make(['asset_tag' => Asset::autoincrement_asset() ]);
$this->assertTrue($a->save());
$this->assertFalse($b->save());
}
public function testAutoIncrementDouble()
{
// make one asset with the autoincrement *ONE* higher than the next auto-increment
// make sure you can then still make another
$this->settings->enableAutoIncrement();
$gap_number = Asset::autoincrement_asset(1);
$final_number = Asset::autoincrement_asset(2);
$a = Asset::factory()->make(['asset_tag' => $gap_number]); //make an asset with an ID that is one *over* the next increment
$b = Asset::factory()->make(['asset_tag' => Asset::autoincrement_asset()]); //but also make one with one that is *at* the next increment
$this->assertTrue($a->save());
$this->assertTrue($b->save());
//and ensure a final asset ends up at *two* over what would've been the next increment at the start
$c = Asset::factory()->make(['asset_tag' => Asset::autoincrement_asset()]);
$this->assertTrue($c->save());
$this->assertEquals($c->asset_tag, $final_number);
}
public function testAutoIncrementGapAndBackfill()
{
// make one asset 3 higher than the next auto-increment
// manually make one that's 1 lower than that
// make sure the next one is one higher than the 3 higher one.
$this->settings->enableAutoIncrement();
$big_gap = Asset::autoincrement_asset(3);
$final_result = Asset::autoincrement_asset(4);
$backfill_one = Asset::autoincrement_asset(0);
$backfill_two = Asset::autoincrement_asset(1);
$backfill_three = Asset::autoincrement_asset(2);
$a = Asset::factory()->create(['asset_tag' => $big_gap]);
$this->assertModelExists($a);
$b = Asset::factory()->create(['asset_tag' => $backfill_one]);
$this->assertModelExists($b);
$c = Asset::factory()->create(['asset_tag' => $backfill_two]);
$this->assertModelExists($c);
$d = Asset::factory()->create(['asset_tag' => $backfill_three]);
$this->assertModelExists($d);
$final = Asset::factory()->create(['asset_tag' => Asset::autoincrement_asset()]);
$this->assertModelExists($final);
$this->assertEquals($final->asset_tag, $final_result);
}
public function testPrefixlessAutoincrementBackfill()
{
// TODO: COPYPASTA FROM above, is there a way to still run this test but not have it be so duplicative?
$this->settings->enableAutoIncrement()->set(['auto_increment_prefix' => '']);
$big_gap = Asset::autoincrement_asset(3);
$final_result = Asset::autoincrement_asset(4);
$backfill_one = Asset::autoincrement_asset(0);
$backfill_two = Asset::autoincrement_asset(1);
$backfill_three = Asset::autoincrement_asset(2);
$a = Asset::factory()->create(['asset_tag' => $big_gap]);
$this->assertModelExists($a);
$b = Asset::factory()->create(['asset_tag' => $backfill_one]);
$this->assertModelExists($b);
$c = Asset::factory()->create(['asset_tag' => $backfill_two]);
$this->assertModelExists($c);
$d = Asset::factory()->create(['asset_tag' => $backfill_three]);
$this->assertModelExists($d);
$final = Asset::factory()->create(['asset_tag' => Asset::autoincrement_asset()]);
$this->assertModelExists($final);
$this->assertEquals($final->asset_tag, $final_result);
}
public function testUnzerofilledPrefixlessAutoincrementBackfill()
{
// TODO: COPYPASTA FROM above (AGAIN), is there a way to still run this test but not have it be so duplicative?
$this->settings->enableAutoIncrement()->set(['auto_increment_prefix' => '','zerofill_count' => 0]);
$big_gap = Asset::autoincrement_asset(3);
$final_result = Asset::autoincrement_asset(4);
$backfill_one = Asset::autoincrement_asset(0);
$backfill_two = Asset::autoincrement_asset(1);
$backfill_three = Asset::autoincrement_asset(2);
$a = Asset::factory()->create(['asset_tag' => $big_gap]);
$this->assertModelExists($a);
$b = Asset::factory()->create(['asset_tag' => $backfill_one]);
$this->assertModelExists($b);
$c = Asset::factory()->create(['asset_tag' => $backfill_two]);
$this->assertModelExists($c);
$d = Asset::factory()->create(['asset_tag' => $backfill_three]);
$this->assertModelExists($d);
$final = Asset::factory()->create(['asset_tag' => Asset::autoincrement_asset()]);
$this->assertModelExists($final);
$this->assertEquals($final->asset_tag, $final_result);
}
public function testWarrantyExpiresAttribute()
{
$asset = Asset::factory()
->create(
[
'model_id' => AssetModel::factory()
->create(
[
'category_id' => Category::factory()->assetLaptopCategory()->create()->id
]
)->id,
'warranty_months' => 24,
'purchase_date' => Carbon::createFromDate(2017, 1, 1)->hour(0)->minute(0)->second(0)
]);
$this->assertEquals(Carbon::createFromDate(2017, 1, 1)->format('Y-m-d'), $asset->purchase_date->format('Y-m-d'));
$this->assertEquals(Carbon::createFromDate(2019, 1, 1)->format('Y-m-d'), $asset->warranty_expires->format('Y-m-d'));
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace Tests\Unit;
use App\Models\Category;
use App\Models\AssetModel;
use App\Models\Asset;
use Tests\TestCase;
class CategoryTest extends TestCase
{
public function testFailsEmptyValidation()
{
// An Asset requires a name, a qty, and a category_id.
$a = Category::create();
$this->assertFalse($a->isValid());
$fields = [
'name' => 'name',
'category_type' => 'category type',
];
$errors = $a->getErrors();
foreach ($fields as $field => $fieldTitle) {
$this->assertEquals($errors->get($field)[0], "The ${fieldTitle} field is required.");
}
}
public function testACategoryCanHaveAssets()
{
$category = Category::factory()->assetDesktopCategory()->create();
// Generate 5 models via factory
$models = AssetModel::factory()
->mbp13Model()
->count(5)
->create(
[
'category_id' => $category->id
]
);
// Loop through the models and create 2 assets in each model
$models->each(function ($model) {
//dd($model);
$asset = Asset::factory()
->count(2)
->create(
[
'model_id' => $model->id,
]
);
//dd($asset);
});
$this->assertCount(5, $category->models);
$this->assertCount(5, $category->models);
$this->assertEquals(10, $category->itemCount());
}
}

View File

@ -0,0 +1,166 @@
<?php
namespace Tests\Unit;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\AssetMaintenance;
use App\Models\Company;
use App\Models\Component;
use App\Models\Consumable;
use App\Models\License;
use App\Models\LicenseSeat;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Tests\TestCase;
class CompanyScopingTest extends TestCase
{
public function models(): array
{
return [
'Accessories' => [Accessory::class],
'Assets' => [Asset::class],
'Components' => [Component::class],
'Consumables' => [Consumable::class],
'Licenses' => [License::class],
];
}
/** @dataProvider models */
public function testCompanyScoping($model)
{
[$companyA, $companyB] = Company::factory()->count(2)->create();
$modelA = $model::factory()->for($companyA)->create();
$modelB = $model::factory()->for($companyB)->create();
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
$userInCompanyA = $companyA->users()->save(User::factory()->make());
$userInCompanyB = $companyB->users()->save(User::factory()->make());
$this->settings->disableMultipleFullCompanySupport();
$this->actingAs($superUser);
$this->assertCanSee($modelA);
$this->assertCanSee($modelB);
$this->actingAs($userInCompanyA);
$this->assertCanSee($modelA);
$this->assertCanSee($modelB);
$this->actingAs($userInCompanyB);
$this->assertCanSee($modelA);
$this->assertCanSee($modelB);
$this->settings->enableMultipleFullCompanySupport();
$this->actingAs($superUser);
$this->assertCanSee($modelA);
$this->assertCanSee($modelB);
$this->actingAs($userInCompanyA);
$this->assertCanSee($modelA);
$this->assertCannotSee($modelB);
$this->actingAs($userInCompanyB);
$this->assertCannotSee($modelA);
$this->assertCanSee($modelB);
}
public function testAssetMaintenanceCompanyScoping()
{
[$companyA, $companyB] = Company::factory()->count(2)->create();
$assetMaintenanceForCompanyA = AssetMaintenance::factory()->for(Asset::factory()->for($companyA))->create();
$assetMaintenanceForCompanyB = AssetMaintenance::factory()->for(Asset::factory()->for($companyB))->create();
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
$userInCompanyA = $companyA->users()->save(User::factory()->make());
$userInCompanyB = $companyB->users()->save(User::factory()->make());
$this->settings->disableMultipleFullCompanySupport();
$this->actingAs($superUser);
$this->assertCanSee($assetMaintenanceForCompanyA);
$this->assertCanSee($assetMaintenanceForCompanyB);
$this->actingAs($userInCompanyA);
$this->assertCanSee($assetMaintenanceForCompanyA);
$this->assertCanSee($assetMaintenanceForCompanyB);
$this->actingAs($userInCompanyB);
$this->assertCanSee($assetMaintenanceForCompanyA);
$this->assertCanSee($assetMaintenanceForCompanyB);
$this->settings->enableMultipleFullCompanySupport();
$this->actingAs($superUser);
$this->assertCanSee($assetMaintenanceForCompanyA);
$this->assertCanSee($assetMaintenanceForCompanyB);
$this->actingAs($userInCompanyA);
$this->assertCanSee($assetMaintenanceForCompanyA);
$this->assertCannotSee($assetMaintenanceForCompanyB);
$this->actingAs($userInCompanyB);
$this->assertCannotSee($assetMaintenanceForCompanyA);
$this->assertCanSee($assetMaintenanceForCompanyB);
}
public function testLicenseSeatCompanyScoping()
{
[$companyA, $companyB] = Company::factory()->count(2)->create();
$licenseSeatA = LicenseSeat::factory()->for(Asset::factory()->for($companyA))->create();
$licenseSeatB = LicenseSeat::factory()->for(Asset::factory()->for($companyB))->create();
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
$userInCompanyA = $companyA->users()->save(User::factory()->make());
$userInCompanyB = $companyB->users()->save(User::factory()->make());
$this->settings->disableMultipleFullCompanySupport();
$this->actingAs($superUser);
$this->assertCanSee($licenseSeatA);
$this->assertCanSee($licenseSeatB);
$this->actingAs($userInCompanyA);
$this->assertCanSee($licenseSeatA);
$this->assertCanSee($licenseSeatB);
$this->actingAs($userInCompanyB);
$this->assertCanSee($licenseSeatA);
$this->assertCanSee($licenseSeatB);
$this->settings->enableMultipleFullCompanySupport();
$this->actingAs($superUser);
$this->assertCanSee($licenseSeatA);
$this->assertCanSee($licenseSeatB);
$this->actingAs($userInCompanyA);
$this->assertCanSee($licenseSeatA);
$this->assertCannotSee($licenseSeatB);
$this->actingAs($userInCompanyB);
$this->assertCannotSee($licenseSeatA);
$this->assertCanSee($licenseSeatB);
}
private function assertCanSee(Model $model)
{
$this->assertTrue(
get_class($model)::all()->contains($model),
'User was not able to see expected model'
);
}
private function assertCannotSee(Model $model)
{
$this->assertFalse(
get_class($model)::all()->contains($model),
'User was able to see model from a different company'
);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Tests\Unit;
use App\Models\Category;
use App\Models\Company;
use App\Models\Component;
use App\Models\Location;
use Tests\TestCase;
class ComponentTest extends TestCase
{
public function testAComponentBelongsToACompany()
{
$component = Component::factory()
->create(
[
'company_id' => Company::factory()->create()->id
]
);
$this->assertInstanceOf(Company::class, $component->company);
}
public function testAComponentHasALocation()
{
$component = Component::factory()
->create(['location_id' => Location::factory()->create()->id]);
$this->assertInstanceOf(Location::class, $component->location);
}
public function testAComponentBelongsToACategory()
{
$component = Component::factory()->ramCrucial4()
->create(
[
'category_id' =>
Category::factory()->create(
[
'category_type' => 'component'
]
)->id]);
$this->assertInstanceOf(Category::class, $component->category);
$this->assertEquals('component', $component->category->category_type);
}
}

View File

@ -0,0 +1,111 @@
<?php
namespace Tests\Unit;
use App\Models\CustomField;
use Tests\TestCase;
/*
* Test strings for db column names gathered from
* http://www.omniglot.com/language/phrases/hovercraft.htm
*/
class CustomFieldTest extends TestCase
{
public function testFormat()
{
$customfield = CustomField::factory()->make(['format' => 'IP']);
$this->assertEquals($customfield->getAttributes()['format'], CustomField::PREDEFINED_FORMATS['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');
}
// Western Europe
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');
}
// Asian
public function testDbNameChinese()
{
$customfield = new CustomField();
$customfield->name = '我的氣墊船裝滿了鱔魚';
$customfield->id = 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()
{
$customfield = new CustomField();
$customfield->name = '私のホバークラフトは鰻でいっぱいです';
$customfield->id = 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 = '내 호버크라프트는 장어로 가득 차 있어요';
$customfield->id = 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';
$customfield->id = 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';
$customfield->id = 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()
{
$customfield = new CustomField();
$customfield->name = 'حَوّامتي مُمْتِلئة بِأَنْقَلَيْسون';
$customfield->id = 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

@ -0,0 +1,44 @@
<?php
namespace Tests\Unit;
use App\Models\Depreciation;
use App\Models\Category;
use App\Models\License;
use App\Models\AssetModel;
use Tests\TestCase;
class DepreciationTest extends TestCase
{
public function testADepreciationHasModels()
{
$depreciation = Depreciation::factory()->create();
AssetModel::factory()
->mbp13Model()
->count(5)
->create(
[
'category_id' => Category::factory()->assetLaptopCategory()->create(),
'depreciation_id' => $depreciation->id
]);
$this->assertEquals(5, $depreciation->models->count());
}
public function testADepreciationHasLicenses()
{
$depreciation = Depreciation::factory()->create();
License::factory()
->count(5)
->photoshop()
->create(
[
'category_id' => Category::factory()->licenseGraphicsCategory()->create(),
'depreciation_id' => $depreciation->id
]);
$this->assertEquals(5, $depreciation->licenses()->count());
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Tests\Unit\Helpers;
use App\Helpers\Helper;
use Tests\TestCase;
class HelperTest extends TestCase
{
public function testDefaultChartColorsMethodHandlesHighValues()
{
$this->assertIsString(Helper::defaultChartColors(1000));
}
public function testDefaultChartColorsMethodHandlesNegativeNumbers()
{
$this->assertIsString(Helper::defaultChartColors(-1));
}
}

View File

@ -0,0 +1,210 @@
<?php
namespace Tests\Unit;
use App\Models\Ldap;
use Tests\TestCase;
/**
* @group ldap
*/
class LdapTest extends TestCase
{
use \phpmock\phpunit\PHPMock;
public function testConnect()
{
$this->settings->enableLdap();
$ldap_connect = $this->getFunctionMock("App\\Models", "ldap_connect");
$ldap_connect->expects($this->once())->willReturn('hello');
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
$ldap_set_option->expects($this->exactly(3));
$blah = Ldap::connectToLdap();
$this->assertEquals('hello',$blah,"LDAP_connect should return 'hello'");
}
// other test cases - with/without client-side certs?
// with/without LDAP version 3?
// with/without ignore cert validation?
// test (and mock) ldap_start_tls() ?
public function testBindAdmin()
{
$this->settings->enableLdap();
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
$this->assertNull(Ldap::bindAdminToLdap("dummy"));
}
public function testBindBad()
{
$this->settings->enableLdap();
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(false);
$this->getFunctionMock("App\\Models","ldap_error")->expects($this->once())->willReturn("exception");
$this->expectExceptionMessage("Could not bind to LDAP:");
$this->assertNull(Ldap::bindAdminToLdap("dummy"));
}
// other test cases - test donked password?
public function testAnonymousBind()
{
//todo - would be nice to introspect somehow to make sure the right parameters were passed?
$this->settings->enableAnonymousLdap();
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
$this->assertNull(Ldap::bindAdminToLdap("dummy"));
}
public function testBadAnonymousBind()
{
$this->settings->enableAnonymousLdap();
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(false);
$this->getFunctionMock("App\\Models","ldap_error")->expects($this->once())->willReturn("exception");
$this->expectExceptionMessage("Could not bind to LDAP:");
$this->assertNull(Ldap::bindAdminToLdap("dummy"));
}
public function testBadEncryptedPassword()
{
$this->settings->enableBadPasswordLdap();
$this->expectExceptionMessage("Your app key has changed");
$this->assertNull(Ldap::bindAdminToLdap("dummy"));
}
public function testFindAndBind()
{
$this->settings->enableLdap();
$ldap_connect = $this->getFunctionMock("App\\Models", "ldap_connect");
$ldap_connect->expects($this->once())->willReturn('hello');
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
$ldap_set_option->expects($this->exactly(3));
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
$this->getFunctionMock("App\\Models", "ldap_search")->expects($this->once())->willReturn(true);
$this->getFunctionMock("App\\Models", "ldap_first_entry")->expects($this->once())->willReturn(true);
$this->getFunctionMock("App\\Models", "ldap_get_attributes")->expects($this->once())->willReturn(
[
"count" => 1,
0 => [
'sn' => 'Surname',
'firstName' => 'FirstName'
]
]
);
$results = Ldap::findAndBindUserLdap("username","password");
$this->assertEqualsCanonicalizing(["count" =>1,0 =>['sn' => 'Surname','firstname' => 'FirstName']],$results);
}
public function testFindAndBindBadPassword()
{
$this->settings->enableLdap();
$ldap_connect = $this->getFunctionMock("App\\Models", "ldap_connect");
$ldap_connect->expects($this->once())->willReturn('hello');
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
$ldap_set_option->expects($this->exactly(3));
// note - we return FALSE first, to simulate a bad-bind, then TRUE the second time to simulate a successful admin bind
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->exactly(2))->willReturn(false, true);
// $this->getFunctionMock("App\\Models","ldap_error")->expects($this->once())->willReturn("exception");
// $this->expectExceptionMessage("exception");
$results = Ldap::findAndBindUserLdap("username","password");
$this->assertFalse($results);
}
public function testFindAndBindCannotFindSelf()
{
$this->settings->enableLdap();
$ldap_connect = $this->getFunctionMock("App\\Models", "ldap_connect");
$ldap_connect->expects($this->once())->willReturn('hello');
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
$ldap_set_option->expects($this->exactly(3));
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
$this->getFunctionMock("App\\Models", "ldap_search")->expects($this->once())->willReturn(false);
$this->expectExceptionMessage("Could not search LDAP:");
$results = Ldap::findAndBindUserLdap("username","password");
$this->assertFalse($results);
}
//maybe should do an AD test as well?
public function testFindLdapUsers()
{
$this->settings->enableLdap();
$ldap_connect = $this->getFunctionMock("App\\Models", "ldap_connect");
$ldap_connect->expects($this->once())->willReturn('hello');
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
$ldap_set_option->expects($this->exactly(3));
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
$this->getFunctionMock("App\\Models", "ldap_search")->expects($this->once())->willReturn(["stuff"]);
$this->getFunctionMock("App\\Models", "ldap_parse_result")->expects($this->once())->willReturn(true);
$this->getFunctionMock("App\\Models", "ldap_get_entries")->expects($this->once())->willReturn(["count" => 1]);
$results = Ldap::findLdapUsers();
$this->assertEqualsCanonicalizing(["count" => 1], $results);
}
public function testFindLdapUsersPaginated()
{
$this->settings->enableLdap();
$ldap_connect = $this->getFunctionMock("App\\Models", "ldap_connect");
$ldap_connect->expects($this->once())->willReturn('hello');
$ldap_set_option = $this->getFunctionMock("App\\Models", "ldap_set_option");
$ldap_set_option->expects($this->exactly(3));
$this->getFunctionMock("App\\Models", "ldap_bind")->expects($this->once())->willReturn(true);
$this->getFunctionMock("App\\Models", "ldap_search")->expects($this->exactly(2))->willReturn(["stuff"]);
$this->getFunctionMock("App\\Models", "ldap_parse_result")->expects($this->exactly(2))->willReturnCallback(
function ($ldapconn, $search_results, $errcode , $matcheddn , $errmsg , $referrals, &$controls) {
static $count = 0;
if($count == 0) {
$count++;
$controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] = "cookie";
return ["count" => 1];
} else {
$controls = [];
return ["count" => 1];
}
}
);
$this->getFunctionMock("App\\Models", "ldap_get_entries")->expects($this->exactly(2))->willReturn(["count" => 1]);
$results = Ldap::findLdapUsers();
$this->assertEqualsCanonicalizing(["count" => 2], $results);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Tests\Unit;
use App\Models\Location;
use Tests\TestCase;
class LocationTest extends TestCase
{
public function testPassesIfNotSelfParent()
{
$a = Location::factory()->make([
'name' => 'Test Location',
'id' => 1,
'parent_id' => Location::factory()->create(['id' => 10])->id,
]);
$this->assertTrue($a->isValid());
}
public function testFailsIfSelfParent()
{
$a = Location::factory()->make([
'name' => 'Test Location',
'id' => 1,
'parent_id' => 1,
]);
$this->assertFalse($a->isValid());
$this->assertStringContainsString(trans('validation.non_circular', ['attribute' => 'parent id']), $a->getErrors());
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Tests\Unit\Models\Company;
use App\Models\Company;
use App\Models\User;
use Tests\TestCase;
class CompanyTest extends TestCase
{
public function testACompanyCanHaveUsers()
{
$company = Company::factory()->create();
$user = User::factory()
->create(
[
'company_id'=> $company->id
]
);
$this->assertCount(1, $company->users);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Tests\Unit\Models\Company;
use App\Models\Company;
use App\Models\User;
use Tests\TestCase;
class GetIdForCurrentUserTest extends TestCase
{
public function testReturnsProvidedValueWhenFullCompanySupportDisabled()
{
$this->settings->disableMultipleFullCompanySupport();
$this->actingAs(User::factory()->create());
$this->assertEquals(1000, Company::getIdForCurrentUser(1000));
}
public function testReturnsProvidedValueForSuperUsersWhenFullCompanySupportEnabled()
{
$this->settings->enableMultipleFullCompanySupport();
$this->actingAs(User::factory()->superuser()->create());
$this->assertEquals(2000, Company::getIdForCurrentUser(2000));
}
public function testReturnsNonSuperUsersCompanyIdWhenFullCompanySupportEnabled()
{
$this->settings->enableMultipleFullCompanySupport();
$this->actingAs(User::factory()->forCompany(['id' => 2000])->create());
$this->assertEquals(2000, Company::getIdForCurrentUser(1000));
}
public function testReturnsProvidedValueForNonSuperUserWithoutCompanyIdWhenFullCompanySupportEnabled()
{
$this->settings->enableMultipleFullCompanySupport();
$this->actingAs(User::factory()->create(['company_id' => null]));
$this->assertEquals(1000, Company::getIdForCurrentUser(1000));
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Tests\Unit\Models\Labels;
use App\Models\Asset;
use App\Models\Labels\FieldOption;
use App\Models\User;
use Tests\TestCase;
class FieldOptionTest extends TestCase
{
public function testItDisplaysAssignedToProperly()
{
// "assignedTo" is a "special" value that can be used in the new label engine
$fieldOption = FieldOption::fromString('Assigned To=assignedTo');
$asset = Asset::factory()
->assignedToUser(User::factory()->create(['first_name' => 'Luke', 'last_name' => 'Skywalker']))
->create();
$this->assertEquals('Luke Skywalker', $fieldOption->getValue($asset));
// If the "assignedTo" relationship was eager loaded then the way to get the
// relationship changes from $asset->assignedTo to $asset->assigned.
$this->assertEquals('Luke Skywalker', $fieldOption->getValue(Asset::with('assignedTo')->find($asset->id)));
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Tests\Unit;
use App\Models\User;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Category;
use Carbon\Carbon;
use App\Notifications\CheckoutAssetNotification;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class NotificationTest extends TestCase
{
public function testAUserIsEmailedIfTheyCheckoutAnAssetWithEULA()
{
$admin = User::factory()->superuser()->create();
$user = User::factory()->create();
$asset = Asset::factory()
->create(
[
'model_id' => AssetModel::factory()
->create(
[
'category_id' => Category::factory()->assetLaptopCategory()->create()->id
]
)->id,
'warranty_months' => 24,
'purchase_date' => Carbon::createFromDate(2017, 1, 1)->hour(0)->minute(0)->second(0)->format('Y-m-d')
]);
Notification::fake();
$asset->checkOut($user, $admin->id);
Notification::assertSentTo($user, CheckoutAssetNotification::class);
}
}

View File

@ -0,0 +1,73 @@
<?php
namespace Tests\Unit;
use App\Models\SnipeModel;
use Tests\TestCase;
class SnipeModelTest extends TestCase
{
public function testSetsPurchaseDatesAppropriately()
{
$c = new SnipeModel;
$c->purchase_date = '';
$this->assertTrue($c->purchase_date === null);
$c->purchase_date = '2016-03-25 12:35:50';
$this->assertTrue($c->purchase_date === '2016-03-25 12:35:50');
}
public function testSetsPurchaseCostsAppropriately()
{
$c = new SnipeModel;
$c->purchase_cost = '0.00';
$this->assertTrue($c->purchase_cost === null);
$c->purchase_cost = '9.54';
$this->assertTrue($c->purchase_cost === 9.54);
$c->purchase_cost = '9.50';
$this->assertTrue($c->purchase_cost === 9.5);
}
public function testNullsBlankLocationIdsButNotOthers()
{
$c = new SnipeModel;
$c->location_id = '';
$this->assertTrue($c->location_id === null);
$c->location_id = '5';
$this->assertTrue($c->location_id == 5);
}
public function testNullsBlankCategoriesButNotOthers()
{
$c = new SnipeModel;
$c->category_id = '';
$this->assertTrue($c->category_id === null);
$c->category_id = '1';
$this->assertTrue($c->category_id == 1);
}
public function testNullsBlankSuppliersButNotOthers()
{
$c = new SnipeModel;
$c->supplier_id = '';
$this->assertTrue($c->supplier_id === null);
$c->supplier_id = '4';
$this->assertTrue($c->supplier_id == 4);
}
public function testNullsBlankDepreciationsButNotOthers()
{
$c = new SnipeModel;
$c->depreciation_id = '';
$this->assertTrue($c->depreciation_id === null);
$c->depreciation_id = '4';
$this->assertTrue($c->depreciation_id == 4);
}
public function testNullsBlankManufacturersButNotOthers()
{
$c = new SnipeModel;
$c->manufacturer_id = '';
$this->assertTrue($c->manufacturer_id === null);
$c->manufacturer_id = '4';
$this->assertTrue($c->manufacturer_id == 4);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Tests\Unit;
use App\Models\Statuslabel;
use Tests\TestCase;
class StatuslabelTest extends TestCase
{
public function testRTDStatuslabelAdd()
{
$statuslabel = Statuslabel::factory()->rtd()->create();
$this->assertModelExists($statuslabel);
}
public function testPendingStatuslabelAdd()
{
$statuslabel = Statuslabel::factory()->pending()->create();
$this->assertModelExists($statuslabel);
}
public function testArchivedStatuslabelAdd()
{
$statuslabel = Statuslabel::factory()->archived()->create();
$this->assertModelExists($statuslabel);
}
public function testOutForRepairStatuslabelAdd()
{
$statuslabel = Statuslabel::factory()->outForRepair()->create();
$this->assertModelExists($statuslabel);
}
public function testBrokenStatuslabelAdd()
{
$statuslabel = Statuslabel::factory()->broken()->create();
$this->assertModelExists($statuslabel);
}
public function testLostStatuslabelAdd()
{
$statuslabel = Statuslabel::factory()->lost()->create();
$this->assertModelExists($statuslabel);
}
}

View File

@ -0,0 +1,98 @@
<?php
namespace Tests\Unit;
use App\Models\User;
use Tests\TestCase;
class UserTest extends TestCase
{
public function testFirstNameSplit()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_firstname = 'Natalia';
$expected_lastname = "Allanovna Romanova-O'Shostakova";
$user = User::generateFormattedNameFromFullName($fullname, 'firstname');
$this->assertEquals($expected_firstname, $user['first_name']);
$this->assertEquals($expected_lastname, $user['last_name']);
}
public function testFirstName()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_username = 'natalia';
$user = User::generateFormattedNameFromFullName($fullname, 'firstname');
$this->assertEquals($expected_username, $user['username']);
}
public function testFirstNameDotLastName()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_username = 'natalia.allanovna-romanova-oshostakova';
$user = User::generateFormattedNameFromFullName($fullname, 'firstname.lastname');
$this->assertEquals($expected_username, $user['username']);
}
public function testLastNameFirstInitial()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_username = 'allanovna-romanova-oshostakovan';
$user = User::generateFormattedNameFromFullName($fullname, 'lastnamefirstinitial');
$this->assertEquals($expected_username, $user['username']);
}
public function testFirstInitialLastName()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_username = 'nallanovna-romanova-oshostakova';
$user = User::generateFormattedNameFromFullName($fullname, 'filastname');
$this->assertEquals($expected_username, $user['username']);
}
public function testFirstInitialUnderscoreLastName()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_username = 'nallanovna-romanova-oshostakova';
$user = User::generateFormattedNameFromFullName($fullname, 'firstinitial_lastname');
$this->assertEquals($expected_username, $user['username']);
}
public function testSingleName()
{
$fullname = 'Natalia';
$expected_username = 'natalia';
$user = User::generateFormattedNameFromFullName($fullname, 'firstname_lastname',);
$this->assertEquals($expected_username, $user['username']);
}
public function firstInitialDotLastname()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_username = 'n.allanovnaromanovaoshostakova';
$user = User::generateFormattedNameFromFullName($fullname, 'firstinitial.lastname');
$this->assertEquals($expected_username, $user['username']);
}
public function lastNameUnderscoreFirstInitial()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_username = 'allanovnaromanovaoshostakova_n';
$user = User::generateFormattedNameFromFullName($fullname, 'lastname_firstinitial');
$this->assertEquals($expected_username, $user['username']);
}
public function firstNameLastName()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_username = 'nataliaallanovnaromanovaoshostakova';
$user = User::generateFormattedNameFromFullName($fullname, 'firstnamelastname');
$this->assertEquals($expected_username, $user['username']);
}
public function firstNameLastInitial()
{
$fullname = "Natalia Allanovna Romanova-O'Shostakova";
$expected_username = 'nataliaa';
$user = User::generateFormattedNameFromFullName($fullname, 'firstnamelastinitial');
$this->assertEquals($expected_username, $user['username']);
}
}