2019-12-13 12:43:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-05-09 10:38:20 +00:00
|
|
|
namespace Tests\Trigger\Shop;
|
2019-12-13 12:43:22 +00:00
|
|
|
|
|
|
|
|
use Faker\Factory;
|
|
|
|
|
use UnitTester;
|
|
|
|
|
use Webkul\Category\Models\Category;
|
|
|
|
|
use Webkul\Category\Models\CategoryTranslation;
|
|
|
|
|
use Webkul\Core\Models\Locale;
|
|
|
|
|
|
|
|
|
|
class TriggerCest
|
|
|
|
|
{
|
|
|
|
|
private $faker;
|
|
|
|
|
|
|
|
|
|
private $parentCategory;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2019-12-13 12:43:22 +00:00
|
|
|
private $category;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2020-01-03 14:22:09 +00:00
|
|
|
private $root2Category;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2020-01-03 14:22:09 +00:00
|
|
|
private $childOfRoot2Category;
|
2019-12-13 12:43:22 +00:00
|
|
|
|
|
|
|
|
private $parentCategoryAttributes;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2019-12-13 12:43:22 +00:00
|
|
|
private $categoryAttributes;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2020-01-03 14:22:09 +00:00
|
|
|
private $root2CategoryAttributes;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2020-01-03 14:22:09 +00:00
|
|
|
private $childOfRoot2CategoryAttributes;
|
2019-12-13 12:43:22 +00:00
|
|
|
|
|
|
|
|
private $parentCategoryName;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2019-12-13 12:43:22 +00:00
|
|
|
private $categoryName;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2020-01-03 14:22:09 +00:00
|
|
|
private $root2CategoryName;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2020-01-03 14:22:09 +00:00
|
|
|
private $childOfRoot2CategoryName;
|
|
|
|
|
|
2019-12-13 12:43:22 +00:00
|
|
|
|
|
|
|
|
/** @var Locale $localeEn */
|
|
|
|
|
private $localeEn;
|
2021-10-05 09:37:21 +00:00
|
|
|
|
2019-12-13 12:43:22 +00:00
|
|
|
/** @var Locale $localeDe */
|
|
|
|
|
private $localeDe;
|
|
|
|
|
|
|
|
|
|
public function _before(UnitTester $I)
|
|
|
|
|
{
|
|
|
|
|
$this->faker = Factory::create();
|
|
|
|
|
|
2020-01-03 14:22:09 +00:00
|
|
|
$rootCategoryTranslation = $I->grabRecord(CategoryTranslation::class, [
|
2021-10-06 12:26:08 +00:00
|
|
|
'slug' => 'root',
|
2020-01-03 14:22:09 +00:00
|
|
|
'locale' => 'en',
|
|
|
|
|
]);
|
2021-10-06 12:26:08 +00:00
|
|
|
$rootCategory = $I->grabRecord(Category::class, [
|
2020-01-03 14:22:09 +00:00
|
|
|
'id' => $rootCategoryTranslation->category_id,
|
|
|
|
|
]);
|
|
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->parentCategoryName = $this->faker->word;
|
|
|
|
|
$this->categoryName = $this->faker->word . $this->faker->randomDigit;
|
|
|
|
|
$this->root2CategoryName = $this->faker->word . $this->faker->randomDigit;
|
2020-01-03 14:22:09 +00:00
|
|
|
$this->childOfRoot2CategoryName = $this->faker->word . $this->faker->randomDigit;
|
2019-12-13 12:43:22 +00:00
|
|
|
|
|
|
|
|
$this->localeEn = $I->grabRecord(Locale::class, [
|
|
|
|
|
'code' => 'en',
|
|
|
|
|
]);
|
|
|
|
|
$this->localeDe = $I->have(Locale::class, [
|
|
|
|
|
'code' => 'de',
|
|
|
|
|
'name' => 'German',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->parentCategoryAttributes = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'parent_id' => $rootCategory->id,
|
|
|
|
|
'position' => 1,
|
|
|
|
|
'status' => 1,
|
2019-12-13 12:43:22 +00:00
|
|
|
$this->localeEn->code => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->parentCategoryName,
|
|
|
|
|
'slug' => strtolower($this->parentCategoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
'description' => $this->parentCategoryName,
|
2021-10-06 12:26:08 +00:00
|
|
|
'locale_id' => $this->localeEn->id,
|
2019-12-13 12:43:22 +00:00
|
|
|
],
|
|
|
|
|
$this->localeDe->code => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->parentCategoryName,
|
|
|
|
|
'slug' => strtolower($this->parentCategoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
'description' => $this->parentCategoryName,
|
2021-10-06 12:26:08 +00:00
|
|
|
'locale_id' => $this->localeDe->id,
|
2019-12-13 12:43:22 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
2021-10-05 09:37:21 +00:00
|
|
|
$this->parentCategory = $I->make(Category::class, $this->parentCategoryAttributes)
|
|
|
|
|
->first();
|
2020-01-03 14:22:09 +00:00
|
|
|
$rootCategory->appendNode($this->parentCategory);
|
2019-12-13 12:43:22 +00:00
|
|
|
$I->assertNotNull($this->parentCategory);
|
|
|
|
|
|
|
|
|
|
$this->categoryAttributes = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'position' => 1,
|
|
|
|
|
'status' => 1,
|
|
|
|
|
'parent_id' => $this->parentCategory->id,
|
2019-12-13 12:43:22 +00:00
|
|
|
$this->localeEn->code => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->categoryName,
|
|
|
|
|
'slug' => strtolower($this->categoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
'description' => $this->categoryName,
|
2021-10-06 12:26:08 +00:00
|
|
|
'locale_id' => $this->localeEn->id,
|
2019-12-13 12:43:22 +00:00
|
|
|
],
|
|
|
|
|
$this->localeDe->code => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->categoryName,
|
|
|
|
|
'slug' => strtolower($this->categoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
'description' => $this->categoryName,
|
2021-10-06 12:26:08 +00:00
|
|
|
'locale_id' => $this->localeDe->id,
|
2019-12-13 12:43:22 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
2021-10-05 09:37:21 +00:00
|
|
|
$this->category = $I->make(Category::class, $this->categoryAttributes)
|
|
|
|
|
->first();
|
2020-01-03 14:22:09 +00:00
|
|
|
$this->parentCategory->appendNode($this->category);
|
2019-12-13 12:43:22 +00:00
|
|
|
$I->assertNotNull($this->category);
|
2020-01-03 14:22:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->root2CategoryAttributes = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'position' => 1,
|
|
|
|
|
'status' => 1,
|
|
|
|
|
'parent_id' => null,
|
2020-01-03 14:22:09 +00:00
|
|
|
$this->localeEn->code => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->root2CategoryName,
|
|
|
|
|
'slug' => strtolower($this->root2CategoryName),
|
2020-01-03 14:22:09 +00:00
|
|
|
'description' => $this->root2CategoryName,
|
2021-10-06 12:26:08 +00:00
|
|
|
'locale_id' => $this->localeEn->id,
|
2020-01-03 14:22:09 +00:00
|
|
|
],
|
|
|
|
|
$this->localeDe->code => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->root2CategoryName,
|
|
|
|
|
'slug' => strtolower($this->root2CategoryName),
|
2020-01-03 14:22:09 +00:00
|
|
|
'description' => $this->root2CategoryName,
|
2021-10-06 12:26:08 +00:00
|
|
|
'locale_id' => $this->localeDe->id,
|
2020-01-03 14:22:09 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
2021-10-05 09:37:21 +00:00
|
|
|
$this->root2Category = $I->make(Category::class, $this->root2CategoryAttributes)
|
|
|
|
|
->first();
|
2020-01-03 14:22:09 +00:00
|
|
|
$this->root2Category->save();
|
|
|
|
|
|
|
|
|
|
$I->assertNotNull($this->root2Category);
|
|
|
|
|
$I->assertNull($this->root2Category->parent_id);
|
|
|
|
|
$I->assertGreaterThan($rootCategory->_rgt, $this->root2Category->_lft);
|
|
|
|
|
|
|
|
|
|
$this->childOfRoot2CategoryAttributes = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'position' => 1,
|
|
|
|
|
'status' => 1,
|
|
|
|
|
'parent_id' => $this->root2Category->id,
|
2020-01-03 14:22:09 +00:00
|
|
|
$this->localeEn->code => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->childOfRoot2CategoryName,
|
|
|
|
|
'slug' => strtolower($this->childOfRoot2CategoryName),
|
2020-01-03 14:22:09 +00:00
|
|
|
'description' => $this->childOfRoot2CategoryName,
|
2021-10-06 12:26:08 +00:00
|
|
|
'locale_id' => $this->localeEn->id,
|
2020-01-03 14:22:09 +00:00
|
|
|
],
|
|
|
|
|
$this->localeDe->code => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->childOfRoot2CategoryName,
|
|
|
|
|
'slug' => strtolower($this->childOfRoot2CategoryName),
|
2020-01-03 14:22:09 +00:00
|
|
|
'description' => $this->childOfRoot2CategoryName,
|
2021-10-06 12:26:08 +00:00
|
|
|
'locale_id' => $this->localeDe->id,
|
2020-01-03 14:22:09 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
2021-10-05 09:37:21 +00:00
|
|
|
$this->childOfRoot2Category = $I->make(Category::class, $this->childOfRoot2CategoryAttributes)
|
|
|
|
|
->first();
|
2020-01-03 14:22:09 +00:00
|
|
|
$this->root2Category->appendNode($this->childOfRoot2Category);
|
|
|
|
|
|
|
|
|
|
$I->assertNotNull($this->childOfRoot2Category);
|
2019-12-13 12:43:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInsertTriggerOnCategoryTranslationsTable(UnitTester $I)
|
|
|
|
|
{
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->parentCategory->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->parentCategoryName,
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->parentCategory->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->parentCategoryName,
|
|
|
|
|
'locale' => $this->localeDe->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->categoryName,
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName) . '/' . strtolower($this->categoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->categoryName,
|
|
|
|
|
'locale' => $this->localeDe->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName) . '/' . strtolower($this->categoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
2020-01-03 14:22:09 +00:00
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->root2Category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->root2CategoryName,
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => '',
|
2020-01-03 14:22:09 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->childOfRoot2Category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->childOfRoot2CategoryName,
|
|
|
|
|
'locale' => $this->localeDe->code,
|
|
|
|
|
'url_path' => strtolower($this->childOfRoot2CategoryName),
|
2020-01-03 14:22:09 +00:00
|
|
|
]);
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->childOfRoot2Category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->childOfRoot2CategoryName,
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => strtolower($this->childOfRoot2CategoryName),
|
2020-01-03 14:22:09 +00:00
|
|
|
]);
|
2019-12-13 12:43:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUpdateTriggersOnCategoryTranslationsTable(UnitTester $I)
|
|
|
|
|
{
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->categoryName,
|
|
|
|
|
'slug' => strtolower($this->categoryName),
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName) . '/' . strtolower($this->categoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->categoryName,
|
|
|
|
|
'slug' => strtolower($this->categoryName),
|
|
|
|
|
'locale' => $this->localeDe->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName) . '/' . strtolower($this->categoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$newCategoryName = $this->faker->word;
|
2019-12-13 12:43:22 +00:00
|
|
|
$this->categoryAttributes[$this->localeDe->code]['name'] = $newCategoryName;
|
|
|
|
|
$this->categoryAttributes[$this->localeDe->code]['slug'] = strtolower($newCategoryName);
|
|
|
|
|
$I->assertTrue($this->category->update($this->categoryAttributes));
|
|
|
|
|
$this->category->refresh();
|
|
|
|
|
|
|
|
|
|
$I->dontSeeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $newCategoryName,
|
|
|
|
|
'slug' => strtolower($this->categoryName),
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName) . '/' . strtolower($this->categoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $newCategoryName,
|
|
|
|
|
'slug' => strtolower($newCategoryName),
|
|
|
|
|
'locale' => $this->localeDe->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName) . '/' . strtolower($newCategoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInsertTriggersOnCategoriesTable(UnitTester $I)
|
|
|
|
|
{
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->parentCategory->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->parentCategoryName,
|
|
|
|
|
'slug' => strtolower($this->parentCategoryName),
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->categoryName,
|
|
|
|
|
'slug' => strtolower($this->categoryName),
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName) . '/' . $this->categoryName,
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUpdateTriggersOnCategoriesTable(UnitTester $I)
|
|
|
|
|
{
|
|
|
|
|
$I->seeRecord(Category::class, [
|
2021-10-06 12:26:08 +00:00
|
|
|
'id' => $this->category->id,
|
2019-12-13 12:43:22 +00:00
|
|
|
'parent_id' => $this->parentCategory->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->categoryName,
|
|
|
|
|
'slug' => strtolower($this->categoryName),
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => strtolower($this->parentCategoryName) . '/' . strtolower($this->categoryName),
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$category2Name = $this->faker->word;
|
2019-12-13 12:43:22 +00:00
|
|
|
$category2Attributes = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'position' => 1,
|
|
|
|
|
'status' => 1,
|
|
|
|
|
'parent_id' => $this->parentCategory->id,
|
2019-12-13 12:43:22 +00:00
|
|
|
$this->localeEn->code => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $category2Name,
|
|
|
|
|
'slug' => strtolower($category2Name),
|
2019-12-13 12:43:22 +00:00
|
|
|
'description' => $category2Name,
|
2021-10-06 12:26:08 +00:00
|
|
|
'locale_id' => $this->localeEn->id,
|
2019-12-13 12:43:22 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$category2 = $I->have(Category::class, $category2Attributes);
|
|
|
|
|
$I->assertNotNull($category2);
|
|
|
|
|
|
|
|
|
|
$this->categoryAttributes['parent_id'] = $category2->id;
|
|
|
|
|
$I->assertTrue($this->category->update($this->categoryAttributes));
|
|
|
|
|
$this->category->refresh();
|
|
|
|
|
|
2021-10-05 09:37:21 +00:00
|
|
|
$expectedUrlPath = strtolower($this->parentCategoryName) . '/' . strtolower($category2Name) . '/' . strtolower($this->categoryName);
|
2019-12-13 12:43:22 +00:00
|
|
|
$I->seeRecord(CategoryTranslation::class, [
|
|
|
|
|
'category_id' => $this->category->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => $this->categoryName,
|
|
|
|
|
'slug' => strtolower($this->categoryName),
|
|
|
|
|
'locale' => $this->localeEn->code,
|
|
|
|
|
'url_path' => $expectedUrlPath,
|
2019-12-13 12:43:22 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|