From 4c65d23e2b5d00f73192035173cfa351df465ab3 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Mon, 31 Mar 2025 00:48:00 -0700 Subject: [PATCH] v0.0.15 - catalags & apis many to many wip --- .../ExternalProductCatalogsController.php | 11 ++- .../Table/ExternalProductCatalogsTable.php | 4 ++ templates/ExternalProductCatalogs/view.php | 41 ++++++++--- .../add.php | 8 +++ .../ExternalProductCatalogs/catalog_form.php | 18 +++++ .../element/ExternalProductCatalogs/form.php | 64 +++++++++++++++++ ...lProductCatalogsProductCatalogsFixture.php | 25 +++++++ ...tCatalogsProductCatalogsControllerTest.php | 71 ++++--------------- 8 files changed, 173 insertions(+), 69 deletions(-) create mode 100644 templates/ExternalProductCatalogsProductCatalogs/add.php create mode 100644 templates/element/ExternalProductCatalogs/catalog_form.php create mode 100644 templates/element/ExternalProductCatalogs/form.php create mode 100644 tests/Fixture/ExternalProductCatalogsProductCatalogsFixture.php diff --git a/src/Controller/ExternalProductCatalogsController.php b/src/Controller/ExternalProductCatalogsController.php index d23d9dc..1363ca1 100644 --- a/src/Controller/ExternalProductCatalogsController.php +++ b/src/Controller/ExternalProductCatalogsController.php @@ -65,8 +65,15 @@ class ExternalProductCatalogsController extends AppController { $externalProductCatalog = $this->ExternalProductCatalogs->newEmptyEntity(); if ($this->request->is('post')) { - $externalProductCatalog = $this->ExternalProductCatalogs->patchEntity($externalProductCatalog, $this->request->getData()); - if ($this->ExternalProductCatalogs->save($externalProductCatalog)) { + $saveOptions = [ + 'associated' => [ + 'ExternalProductCatalogsProductCatalogs', + ], + ]; + $postData = $this->request->getData(); + + $externalProductCatalog = $this->ExternalProductCatalogs->patchEntity($externalProductCatalog, $postData, $saveOptions); + if ($this->ExternalProductCatalogs->save($externalProductCatalog, $saveOptions)) { $this->Flash->success(__('The external product catalog has been saved.')); return $this->redirect(['action' => 'index']); diff --git a/src/Model/Table/ExternalProductCatalogsTable.php b/src/Model/Table/ExternalProductCatalogsTable.php index 4646a3a..d87ddd4 100644 --- a/src/Model/Table/ExternalProductCatalogsTable.php +++ b/src/Model/Table/ExternalProductCatalogsTable.php @@ -58,6 +58,10 @@ class ExternalProductCatalogsTable extends Table 'through' => 'CakeProducts.ExternalProductCatalogsProductCatalogs', 'className' => 'CakeProducts.ProductCatalogs', ]); + + $this->hasMany('ExternalProductCatalogsProductCatalogs', [ + 'className' => 'CakeProducts.ExternalProductCatalogsProductCatalogs', + ]); } /** diff --git a/templates/ExternalProductCatalogs/view.php b/templates/ExternalProductCatalogs/view.php index fbe1c5d..47bd4ac 100644 --- a/templates/ExternalProductCatalogs/view.php +++ b/templates/ExternalProductCatalogs/view.php @@ -1,7 +1,7 @@
@@ -18,10 +18,6 @@

base_url) ?>

- - - - @@ -30,6 +26,10 @@ + + + + @@ -38,11 +38,34 @@ - - - -
hasValue('product_catalog') ? $this->Html->link($externalProductCatalog->product_catalog->name, ['controller' => 'ProductCatalogs', 'action' => 'view', $externalProductCatalog->product_catalog->id]) : '' ?>
base_url) ?> api_url) ?>
Number->format($externalProductCatalog->id) ?>
created) ?> deleted) ?>
enabled ? __('Yes') : __('No'); ?>
+
diff --git a/templates/ExternalProductCatalogsProductCatalogs/add.php b/templates/ExternalProductCatalogsProductCatalogs/add.php new file mode 100644 index 0000000..c2d5ea0 --- /dev/null +++ b/templates/ExternalProductCatalogsProductCatalogs/add.php @@ -0,0 +1,8 @@ +setLayout('ajax'); + +echo $this->element('ExternalProductCatalogs/catalog_form'); ?> diff --git a/templates/element/ExternalProductCatalogs/catalog_form.php b/templates/element/ExternalProductCatalogs/catalog_form.php new file mode 100644 index 0000000..59b9482 --- /dev/null +++ b/templates/element/ExternalProductCatalogs/catalog_form.php @@ -0,0 +1,18 @@ +request->getQuery('catalog_prefix') !== null) { +$prefix = 'external_product_catalogs_product_catalogs.' . $this->request->getQuery('catalog_prefix') . '.'; +} +echo '
'; +echo $this->Form->control($prefix . 'product_catalog_id', ['options' => $productCatalogs]); +?> diff --git a/templates/element/ExternalProductCatalogs/form.php b/templates/element/ExternalProductCatalogs/form.php new file mode 100644 index 0000000..d8bcffc --- /dev/null +++ b/templates/element/ExternalProductCatalogs/form.php @@ -0,0 +1,64 @@ +hasValue('product_catalogs') || empty($externalProductCatalog->product_catalogs) ? 0 : count($externalProductCatalog->product_catalogs); + +?> +

API Info

+Form->control('base_url'); +echo $this->Form->control('api_url'); + +?> +
+
+

Product Catalogs + + Html->link('Add Catalog', '#', [ + 'id' => 'add-product-catalog-button', + ]); ?> + +

+Form->number('catalog_prefix', [ + 'value' => $numProductCatalogs - 1, + 'id' => 'catalog_prefix', + 'style' => 'display:none;', + 'hx-get' => $this->Url->build([ + 'plugin' => false, + 'controller' => 'ExternalProductCatalogsProductCatalogs', + 'action' => 'add', + ]), + 'hx-trigger' => 'change', + 'hx-target' => '#product-catalogs-container', + 'hx-swap' => 'beforeend', + 'data-test' => 1, +]); ?> + +
+ hasValue('product_catalogs')) : ?> + product_catalogs as $productCatalog) { + $prefix = 'external_product_catalogs_product_catalogs.' . $cnt . '.'; + echo '
'; + + echo $this->element('ExternalProductCatalogs/catalog_form', [ + 'productCatalog' => $productCatalog, + 'prefix' => $prefix + ]); + $cnt++; + } ?> + +
+Html->script('CakeProducts.product_catalogs.js'); ?> diff --git a/tests/Fixture/ExternalProductCatalogsProductCatalogsFixture.php b/tests/Fixture/ExternalProductCatalogsProductCatalogsFixture.php new file mode 100644 index 0000000..471cbc5 --- /dev/null +++ b/tests/Fixture/ExternalProductCatalogsProductCatalogsFixture.php @@ -0,0 +1,25 @@ +records = [ + + ]; + parent::init(); + } +} diff --git a/tests/TestCase/Controller/ExternalProductCatalogsProductCatalogsControllerTest.php b/tests/TestCase/Controller/ExternalProductCatalogsProductCatalogsControllerTest.php index 914581c..10b78d5 100644 --- a/tests/TestCase/Controller/ExternalProductCatalogsProductCatalogsControllerTest.php +++ b/tests/TestCase/Controller/ExternalProductCatalogsProductCatalogsControllerTest.php @@ -4,8 +4,10 @@ declare(strict_types=1); namespace CakeProducts\Test\TestCase\Controller; use App\Controller\ExternalProductCatalogsProductCatalogsController; +use Cake\ORM\Table; use Cake\TestSuite\IntegrationTestTrait; use Cake\TestSuite\TestCase; +use CakeProducts\Model\Table\ExternalProductCatalogsProductCatalogsTable; use PHPUnit\Exception; /** @@ -15,15 +17,21 @@ use PHPUnit\Exception; */ class ExternalProductCatalogsProductCatalogsControllerTest extends BaseControllerTest { + /** + * Test subject table + * + * @var ExternalProductCatalogsProductCatalogsTable|Table + */ + protected $ExternalProductCatalogsProductCatalogs; /** * Fixtures * * @var array */ protected array $fixtures = [ - 'CakeProducts.ExternalProductCatalogsProductCatalogs', - 'CakeProducts.ExternalProductCatalogs', - 'CakeProducts.ExternalProductCatalogs', + 'plugin.CakeProducts.ExternalProductCatalogsProductCatalogs', + 'plugin.CakeProducts.ExternalProductCatalogs', + 'plugin.CakeProducts.ProductCatalogs', ]; /** @@ -68,6 +76,7 @@ class ExternalProductCatalogsProductCatalogsControllerTest extends BaseControlle // $this->loginUserByRole('admin'); $url = [ + 'plugin' => 'CakeProducts', 'controller' => 'ExternalProductCatalogsProductCatalogs', 'action' => 'add', ]; @@ -78,61 +87,6 @@ class ExternalProductCatalogsProductCatalogsControllerTest extends BaseControlle $this->assertEquals($cntBefore, $cntAfter); } - /** - * Test add method - * - * Tests a POST request to the add action with a logged in user - * - * @uses \App\Controller\ExternalProductCatalogsProductCatalogsController::add() - * @throws Exception - * - * @return void - */ - public function testAddPostSuccess(): void - { - $cntBefore = $this->ExternalProductCatalogsProductCatalogs->find()->count(); - - // $this->loginUserByRole('admin'); - $url = [ - 'controller' => 'ExternalProductCatalogsProductCatalogs', - 'action' => 'add', - ]; - $data = []; - $this->post($url, $data); - $this->assertResponseCode(302); - $this->assertRedirectContains('externalproductcatalogsproductcatalogs/view'); - - $cntAfter = $this->ExternalProductCatalogsProductCatalogs->find()->count(); - $this->assertEquals($cntBefore + 1, $cntAfter); - } - - /** - * Test add method - * - * Tests a POST request to the add action with a logged in user - * - * @uses \App\Controller\ExternalProductCatalogsProductCatalogsController::add() - * @throws Exception - * - * @return void - */ - public function testAddPostFailure(): void - { - $cntBefore = $this->ExternalProductCatalogsProductCatalogs->find()->count(); - - // $this->loginUserByRole('admin'); - $url = [ - 'controller' => 'ExternalProductCatalogsProductCatalogs', - 'action' => 'add', - ]; - $data = []; - $this->post($url, $data); - $this->assertResponseCode(200); - - $cntAfter = $this->ExternalProductCatalogsProductCatalogs->find()->count(); - $this->assertEquals($cntBefore, $cntAfter); - } - /** * Test delete method * @@ -149,6 +103,7 @@ class ExternalProductCatalogsProductCatalogsControllerTest extends BaseControlle // $this->loginUserByRole('admin'); $url = [ + 'plugin' => 'CakeProducts', 'controller' => 'ExternalProductCatalogsProductCatalogs', 'action' => 'delete', 1,