*/ protected array $fixtures = [ 'plugin.CakeProducts.Products', 'plugin.CakeProducts.ProductCategories', // 'plugin.CakeProducts.ProductCatalogs', ]; /** * setUp method * * @return void */ protected function setUp(): void { parent::setUp(); $this->enableCsrfToken(); $this->enableSecurityToken(); $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 an unauthenticated user (not logged in) * * @return void * @throws Exception * * @uses ProductsController::index */ public function testIndexGetUnauthenticated(): void { $url = [ 'plugin' => 'CakeProducts', 'controller' => 'Products', 'action' => 'index', ]; $this->get($url); $this->assertResponseCode(302); $this->assertRedirectContains('login'); } /** * Test index method * * Tests the index action with a logged in user * * @return void * @throws Exception * * @uses ProductsController::index */ public function testIndexGetLoggedIn(): void { $this->loginUserByRole('admin'); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'Products', 'action' => 'index', ]; $this->get($url); $this->assertResponseCode(200); } /** * Test view method * * Tests the view action with an unauthenticated user (not logged in) * * @return void * @throws Exception * * @uses ProductsController::view */ public function testViewGetUnauthenticated(): void { $id = 'cfc98a9a-29b2-44c8-b587-8156adc05317'; $url = [ 'plugin' => 'CakeProducts', 'controller' => 'Products', 'action' => 'view', $id, ]; $this->get($url); $this->assertResponseCode(302); $this->assertRedirectContains('login'); } /** * Test view method * * Tests the view action with a logged in user * * @return void * @throws Exception * * @uses ProductsController::view */ public function testViewGetLoggedIn(): void { $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 an unauthenticated user (not logged in) * * @return void * @throws Exception * * @uses ProductsController::add */ public function testAddGetUnauthenticated(): void { $cntBefore = $this->Products->find()->count(); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'Products', 'action' => 'add', ]; $this->get($url); $this->assertResponseCode(302); $this->assertRedirectContains('login'); $cntAfter = $this->Products->find()->count(); $this->assertEquals($cntBefore, $cntAfter); } /** * Test add method * * Tests the add action with a logged in user * * @return void * @throws Exception * * @uses ProductsController::add */ public function testAddGetLoggedIn(): void { $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 */ public function testAddPostLoggedInSuccess(): void { $cntBefore = $this->Products->find()->count(); $this->loginUserByRole('admin'); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'Products', 'action' => 'add', ]; $data = [ 'product_catalog_id' => '115153f3-2f59-4234-8ff8-e1b205761428', 'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23', 'name' => '16AWG WIRE RED', 'product_type_id' => 1, ]; $this->post($url, $data); $this->assertResponseCode(302); $this->assertRedirectContains('products'); $cntAfter = $this->Products->find()->count(); $this->assertEquals($cntBefore + 1, $cntAfter); } /** * Test add method * * Tests a POST request to the add action with a logged in user * * @return void * @throws Exception * * @uses ProductsController::add */ public function testAddPostLoggedInFailure(): void { $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 an unauthenticated user (not logged in) * * @return void * @throws Exception * * @uses ProductsController::edit */ public function testEditGetUnauthenticated(): void { $url = [ 'plugin' => 'CakeProducts', 'controller' => 'Products', 'action' => 'edit', 'cfc98a9a-29b2-44c8-b587-8156adc05317', ]; $this->get($url); $this->assertResponseCode(302); $this->assertRedirectContains('login'); } /** * Test edit method * * Tests the edit action with a logged in user * * @return void * @throws Exception * * @uses ProductsController::edit */ public function testEditGetLoggedIn(): void { $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 */ public function testEditPutLoggedInSuccess(): void { $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 */ public function testEditPutLoggedInFailure(): void { $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 an unauthenticated user (not logged in) * * @return void * @throws Exception * * @uses ProductsController::delete */ public function testDeleteUnauthenticated(): void { $cntBefore = $this->Products->find()->count(); $url = [ 'plugin' => 'CakeProducts', 'controller' => 'Products', 'action' => 'delete', 'cfc98a9a-29b2-44c8-b587-8156adc05317', ]; $this->delete($url); $this->assertResponseCode(302); $this->assertRedirectContains('login'); $cntAfter = $this->Products->find()->count(); $this->assertEquals($cntBefore, $cntAfter); } /** * Test delete method * * Tests the delete action with a logged in user * * @return void *@throws Exception * * @uses ProductsController::delete */ public function testDeleteLoggedIn(): void { $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); } }