| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace CakeProducts\Test\TestCase\Controller; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Cake\ORM\Table; | 
					
						
							|  |  |  | use Cake\TestSuite\IntegrationTestTrait; | 
					
						
							|  |  |  | use Cake\TestSuite\TestCase; | 
					
						
							|  |  |  | use CakeProducts\Controller\ProductsController; | 
					
						
							|  |  |  | use CakeProducts\Model\Table\ProductCatalogsTable; | 
					
						
							|  |  |  | use CakeProducts\Model\Table\ProductsTable; | 
					
						
							|  |  |  | use PHPUnit\Exception; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * CakeProducts\Controller\ProductsController Test Case | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @uses ProductsController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ProductsControllerTest extends BaseControllerTest | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Test subject table | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var ProductsTable|Table | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $Products; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Fixtures | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array<string> | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected array $fixtures = [ | 
					
						
							|  |  |  |         'plugin.CakeProducts.Products', | 
					
						
							|  |  |  |         'plugin.CakeProducts.ProductCategories', | 
					
						
							|  |  |  | //        'plugin.CakeProducts.ProductCatalogs',
 | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * setUp method | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function setUp(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::setUp(); | 
					
						
							| 
									
										
										
										
											2025-03-27 08:44:16 +00:00
										 |  |  | //        $this->enableCsrfToken();
 | 
					
						
							|  |  |  | //        $this->enableSecurityToken();
 | 
					
						
							| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  |         $config = $this->getTableLocator()->exists('Products') ? [] : ['className' => ProductsTable::class]; | 
					
						
							|  |  |  |         $this->Products = $this->getTableLocator()->get('Products', $config); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * tearDown method | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function tearDown(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         unset($this->Products); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parent::tearDown(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Test index method | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * Tests the index action with a logged in user | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @return void | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @uses ProductsController::index | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-03-27 08:11:41 +00:00
										 |  |  |     public function testIndexGet(): void | 
					
						
							| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->loginUserByRole('admin'); | 
					
						
							|  |  |  |         $url = [ | 
					
						
							|  |  |  |             'plugin' => 'CakeProducts', | 
					
						
							|  |  |  |             'controller' => 'Products', | 
					
						
							|  |  |  |             'action' => 'index', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->get($url); | 
					
						
							|  |  |  |         $this->assertResponseCode(200); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Test view method | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * Tests the view action with a logged in user | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @return void | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @uses ProductsController::view | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-03-27 08:11:41 +00:00
										 |  |  |     public function testViewGet(): void | 
					
						
							| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $id = 'cfc98a9a-29b2-44c8-b587-8156adc05317'; | 
					
						
							|  |  |  |         $this->loginUserByRole('admin'); | 
					
						
							|  |  |  |         $url = [ | 
					
						
							|  |  |  |             'plugin' => 'CakeProducts', | 
					
						
							|  |  |  |             'controller' => 'Products', | 
					
						
							|  |  |  |             'action' => 'view', | 
					
						
							|  |  |  |             $id, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->get($url); | 
					
						
							|  |  |  |         $this->assertResponseCode(200); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Test add method | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * Tests the add action with a logged in user | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @return void | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @uses ProductsController::add | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-03-27 08:11:41 +00:00
										 |  |  |     public function testAddGet(): void | 
					
						
							| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $cntBefore = $this->Products->find()->count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->loginUserByRole('admin'); | 
					
						
							|  |  |  |         $url = [ | 
					
						
							|  |  |  |             'plugin' => 'CakeProducts', | 
					
						
							|  |  |  |             'controller' => 'Products', | 
					
						
							|  |  |  |             'action' => 'add', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->get($url); | 
					
						
							|  |  |  |         $this->assertResponseCode(200); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cntAfter = $this->Products->find()->count(); | 
					
						
							|  |  |  |         $this->assertEquals($cntBefore, $cntAfter); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Test add method | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * Tests a POST request to the add action with a logged in user | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @return void | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @uses ProductsController::add | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-03-27 08:11:41 +00:00
										 |  |  |     public function testAddPostFailure(): void | 
					
						
							| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $cntBefore = $this->Products->find()->count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->loginUserByRole('admin'); | 
					
						
							|  |  |  |         $url = [ | 
					
						
							|  |  |  |             'plugin' => 'CakeProducts', | 
					
						
							|  |  |  |             'controller' => 'Products', | 
					
						
							|  |  |  |             'action' => 'add', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $data = [ | 
					
						
							|  |  |  |             'product_catalog_id' => '', | 
					
						
							|  |  |  |             'product_category_id' => '', | 
					
						
							|  |  |  |             'name' => '', | 
					
						
							|  |  |  |             'product_type_id' => 1, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->post($url, $data); | 
					
						
							|  |  |  |         $this->assertResponseCode(200); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cntAfter = $this->Products->find()->count(); | 
					
						
							|  |  |  |         $this->assertEquals($cntBefore, $cntAfter); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Test edit method | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * Tests the edit action with a logged in user | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @return void | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |     * @uses ProductsController::edit | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-03-27 08:11:41 +00:00
										 |  |  |     public function testEditGet(): void | 
					
						
							| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->loginUserByRole('admin'); | 
					
						
							|  |  |  |         $url = [ | 
					
						
							|  |  |  |             'plugin' => 'CakeProducts', | 
					
						
							|  |  |  |             'controller' => 'Products', | 
					
						
							|  |  |  |             'action' => 'edit', | 
					
						
							|  |  |  |             'cfc98a9a-29b2-44c8-b587-8156adc05317', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->get($url); | 
					
						
							|  |  |  |         $this->assertResponseCode(200); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Test edit method | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * Tests a PUT request to the edit action with a logged in user | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @return void | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |     * @uses ProductsController::edit | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-03-27 08:11:41 +00:00
										 |  |  |     public function testEditPutSuccess(): void | 
					
						
							| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->loginUserByRole('admin'); | 
					
						
							|  |  |  |         $id = 'cfc98a9a-29b2-44c8-b587-8156adc05317'; | 
					
						
							|  |  |  |         $before = $this->Products->get($id); | 
					
						
							|  |  |  |         $url = [ | 
					
						
							|  |  |  |             'plugin' => 'CakeProducts', | 
					
						
							|  |  |  |             'controller' => 'Products', | 
					
						
							|  |  |  |             'action' => 'edit', | 
					
						
							|  |  |  |             $id, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $data = [ | 
					
						
							|  |  |  |             // test new data here
 | 
					
						
							|  |  |  |             'product_catalog_id' => '115153f3-2f59-4234-8ff8-e1b205761428', | 
					
						
							|  |  |  |             'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23', | 
					
						
							|  |  |  |             'name' => 'edited product name', | 
					
						
							|  |  |  |             'product_type_id' => 1, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->put($url, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertResponseCode(302); | 
					
						
							|  |  |  |         $this->assertRedirectContains('products'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $after = $this->Products->get($id); | 
					
						
							|  |  |  |         $this->assertEquals($data['name'], $after->name); | 
					
						
							|  |  |  |         // assert saved properly below
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Test edit method | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * Tests a PUT request to the edit action with a logged in user | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @return void | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |     * @uses ProductsController::edit | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-03-27 08:11:41 +00:00
										 |  |  |     public function testEditPutFailure(): void | 
					
						
							| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->loginUserByRole('admin'); | 
					
						
							|  |  |  |         $id = 'cfc98a9a-29b2-44c8-b587-8156adc05317'; | 
					
						
							|  |  |  |         $before = $this->Products->get($id); | 
					
						
							|  |  |  |         $url = [ | 
					
						
							|  |  |  |             'plugin' => 'CakeProducts', | 
					
						
							|  |  |  |             'controller' => 'Products', | 
					
						
							|  |  |  |             'action' => 'edit', | 
					
						
							|  |  |  |             $id, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $data = [ | 
					
						
							|  |  |  |             'product_catalog_id' => '', | 
					
						
							|  |  |  |             'product_category_id' => '', | 
					
						
							|  |  |  |             'name' => 'edited name not gonna take', | 
					
						
							|  |  |  |             'product_type_id' => 1, | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->put($url, $data); | 
					
						
							|  |  |  |         $this->assertResponseCode(200); | 
					
						
							|  |  |  |         $after = $this->Products->get($id); | 
					
						
							|  |  |  |         $this->assertEquals($before->name, $after->name); | 
					
						
							|  |  |  |         $this->assertEquals($before->product_category_id, $after->product_category_id); | 
					
						
							|  |  |  |         // assert save failed below
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |     * Test delete method | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * Tests the delete action with a logged in user | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     * @return void | 
					
						
							|  |  |  |     *@throws Exception | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @uses ProductsController::delete | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2025-03-27 08:11:41 +00:00
										 |  |  |     public function testDelete(): void | 
					
						
							| 
									
										
										
										
											2024-11-25 02:38:29 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $cntBefore = $this->Products->find()->count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->loginUserByRole('admin'); | 
					
						
							|  |  |  |         $url = [ | 
					
						
							|  |  |  |             'plugin' => 'CakeProducts', | 
					
						
							|  |  |  |             'controller' => 'Products', | 
					
						
							|  |  |  |             'action' => 'delete', | 
					
						
							|  |  |  |             'cfc98a9a-29b2-44c8-b587-8156adc05317', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->delete($url); | 
					
						
							|  |  |  |         $this->assertResponseCode(302); | 
					
						
							|  |  |  |         $this->assertRedirectContains('products'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cntAfter = $this->Products->find()->count(); | 
					
						
							|  |  |  |         $this->assertEquals($cntBefore - 1, $cntAfter); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |