375 lines
		
	
	
		
			9.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			375 lines
		
	
	
		
			9.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
<?php
 | 
						|
declare(strict_types=1);
 | 
						|
 | 
						|
namespace CakeProducts\Test\TestCase\Controller;
 | 
						|
 | 
						|
use Cake\Core\Configure;
 | 
						|
use Cake\ORM\Table;
 | 
						|
use CakeProducts\Controller\ProductPhotosController;
 | 
						|
use CakeProducts\Model\Table\ProductPhotosTable;
 | 
						|
use CakeProducts\Model\Table\ProductsTable;
 | 
						|
use FilesystemIterator;
 | 
						|
use Laminas\Diactoros\UploadedFile;
 | 
						|
use PHPUnit\Exception;
 | 
						|
use RecursiveDirectoryIterator;
 | 
						|
use RecursiveIteratorIterator;
 | 
						|
use const UPLOAD_ERR_OK;
 | 
						|
 | 
						|
/**
 | 
						|
 * CakeProducts\Controller\ProductPhotosController Test Case
 | 
						|
 *
 | 
						|
 * @uses ProductPhotosController
 | 
						|
 */
 | 
						|
class ProductPhotosControllerTest extends BaseControllerTest
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * Test subject table
 | 
						|
     *
 | 
						|
     * @var ProductPhotosTable|Table
 | 
						|
     */
 | 
						|
    protected $ProductPhotos;
 | 
						|
 | 
						|
    /**
 | 
						|
     * Fixtures
 | 
						|
     *
 | 
						|
     * @var array<string>
 | 
						|
     */
 | 
						|
    protected array $fixtures = [
 | 
						|
        'plugin.CakeProducts.Products',
 | 
						|
        'plugin.CakeProducts.ProductSkus',
 | 
						|
        'plugin.CakeProducts.ProductPhotos',
 | 
						|
    ];
 | 
						|
 | 
						|
    /**
 | 
						|
     * setUp method
 | 
						|
     *
 | 
						|
     * @return void
 | 
						|
     */
 | 
						|
    protected function setUp(): void
 | 
						|
    {
 | 
						|
        parent::setUp();
 | 
						|
        $config = $this->getTableLocator()->exists('ProductPhotos') ? [] : ['className' => ProductPhotosTable::class];
 | 
						|
        $this->ProductPhotos = $this->getTableLocator()->get('ProductPhotos', $config);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * tearDown method
 | 
						|
     *
 | 
						|
     * @return void
 | 
						|
     */
 | 
						|
    protected function tearDown(): void
 | 
						|
    {
 | 
						|
        unset($this->ProductPhotos);
 | 
						|
 | 
						|
        parent::tearDown();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
    * Test index method
 | 
						|
    *
 | 
						|
    * Tests the index action with a logged in user
 | 
						|
    *
 | 
						|
    * @return void
 | 
						|
     * @throws Exception
 | 
						|
    *
 | 
						|
    * @uses ProductPhotosController::index
 | 
						|
    */
 | 
						|
    public function testIndexGetLoggedIn(): void
 | 
						|
    {
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            '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 ProductPhotosController::view
 | 
						|
    */
 | 
						|
    public function testViewGetLoggedIn(): void
 | 
						|
    {
 | 
						|
        $id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            '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 ProductPhotosController::add
 | 
						|
    */
 | 
						|
    public function testAddGetLoggedIn(): void
 | 
						|
    {
 | 
						|
        $cntBefore = $this->ProductPhotos->find()->count();
 | 
						|
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            'action' => 'add',
 | 
						|
        ];
 | 
						|
        $this->get($url);
 | 
						|
        $this->assertResponseCode(200);
 | 
						|
 | 
						|
        $cntAfter = $this->ProductPhotos->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 ProductPhotosController::add
 | 
						|
    */
 | 
						|
    public function testAddPostLoggedInSuccess(): void
 | 
						|
    {
 | 
						|
        $cntBefore = $this->ProductPhotos->find()->count();
 | 
						|
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            'action' => 'add',
 | 
						|
        ];
 | 
						|
        $file = Configure::readOrFail('App.paths.testWebroot') . 'images' . DS . 'cake_icon.png';
 | 
						|
        $toUseFile = Configure::readOrFail('App.paths.testWebroot') . 'images' . DS . 'cake_icon_copy.png';
 | 
						|
        if (!file_exists($file)) {
 | 
						|
            $this->fail('Test image did not exist');
 | 
						|
        }
 | 
						|
        if (!copy($file, $toUseFile)) {
 | 
						|
            $this->fail('Failed to copy test image');
 | 
						|
        }
 | 
						|
        $image = new UploadedFile(
 | 
						|
            $toUseFile, // stream or path to file representing the temp file
 | 
						|
            12345,                    // the filesize in bytes
 | 
						|
            UPLOAD_ERR_OK,           // the upload/error status
 | 
						|
            'cake_icon.png',             // the filename as sent by the client
 | 
						|
            'image/png'              // the mimetype as sent by the client
 | 
						|
        );
 | 
						|
        $this->configRequest([
 | 
						|
            'files' => [
 | 
						|
                'photo' => $image,
 | 
						|
            ],
 | 
						|
        ]);
 | 
						|
        $data = [
 | 
						|
            'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
 | 
						|
            'product_sku_id' => '',
 | 
						|
            'primary_photo' => 0,
 | 
						|
            'photo' => $image,
 | 
						|
            'enabled' => 1,
 | 
						|
        ];
 | 
						|
        $this->post($url, $data);
 | 
						|
        $this->assertResponseCode(302);
 | 
						|
        $this->assertRedirectContains('product-photos');
 | 
						|
 | 
						|
        $cntAfter = $this->ProductPhotos->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 ProductPhotosController::add
 | 
						|
    */
 | 
						|
    public function testAddPostLoggedInFailure(): void
 | 
						|
    {
 | 
						|
        $cntBefore = $this->ProductPhotos->find()->count();
 | 
						|
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            'action' => 'add',
 | 
						|
        ];
 | 
						|
        $data = [
 | 
						|
        ];
 | 
						|
        $this->post($url, $data);
 | 
						|
        $this->assertResponseCode(200);
 | 
						|
 | 
						|
        $cntAfter = $this->ProductPhotos->find()->count();
 | 
						|
        $this->assertEquals($cntBefore, $cntAfter);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
    * Test edit method
 | 
						|
    *
 | 
						|
    * Tests the edit action with a logged in user
 | 
						|
    *
 | 
						|
    * @return void
 | 
						|
     * @throws Exception
 | 
						|
     *
 | 
						|
    * @uses ProductPhotosController::edit
 | 
						|
    */
 | 
						|
    public function testEditGetLoggedIn(): void
 | 
						|
    {
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
 | 
						|
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            'action' => 'edit',
 | 
						|
            $id,
 | 
						|
        ];
 | 
						|
        $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 ProductPhotosController::edit
 | 
						|
    */
 | 
						|
    public function testEditPutLoggedInSuccess(): void
 | 
						|
    {
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
 | 
						|
        $before = $this->ProductPhotos->get($id);
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            'action' => 'edit',
 | 
						|
            $id,
 | 
						|
        ];
 | 
						|
        $data = [
 | 
						|
            'enabled' => 1,
 | 
						|
            'primary_photo' => 1,
 | 
						|
            'photo_position' => 999,
 | 
						|
        ];
 | 
						|
        $this->put($url, $data);
 | 
						|
 | 
						|
        $this->assertResponseCode(302);
 | 
						|
        $this->assertRedirectContains('product-photos');
 | 
						|
 | 
						|
        $after = $this->ProductPhotos->get($id);
 | 
						|
        $this->assertEquals($data['photo_position'], $after->photo_position);
 | 
						|
        $this->assertTrue($after->primary_photo);
 | 
						|
        $this->assertTrue($after->enabled);
 | 
						|
        // 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 ProductPhotosController::edit
 | 
						|
    */
 | 
						|
    public function testEditPutLoggedInFailure(): void
 | 
						|
    {
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
 | 
						|
        $before = $this->ProductPhotos->get($id);
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            'action' => 'edit',
 | 
						|
            $id,
 | 
						|
        ];
 | 
						|
        $data = [
 | 
						|
            'enabled' => 1,
 | 
						|
            'primary_photo' => 1,
 | 
						|
            'photo_position' => 'not-valid',
 | 
						|
        ];
 | 
						|
        $this->put($url, $data);
 | 
						|
        $this->assertResponseCode(200);
 | 
						|
        $after = $this->ProductPhotos->get($id);
 | 
						|
        $this->assertEquals($before->modified, $after->modified);
 | 
						|
        $this->assertEquals($before->photo_position, $after->photo_position);
 | 
						|
 | 
						|
        // assert save failed below
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
    * Test delete method
 | 
						|
    *
 | 
						|
    * Tests the delete action with a logged in user
 | 
						|
    *
 | 
						|
    * @return void
 | 
						|
     * @throws Exception
 | 
						|
     *
 | 
						|
     * @uses ProductPhotosController::delete
 | 
						|
    */
 | 
						|
    public function testDeleteLoggedIn(): void
 | 
						|
    {
 | 
						|
        $cntBefore = $this->ProductPhotos->find()->count();
 | 
						|
        $id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            'action' => 'delete',
 | 
						|
            $id,
 | 
						|
        ];
 | 
						|
        $this->delete($url);
 | 
						|
        $this->assertResponseCode(302);
 | 
						|
        $this->assertRedirectContains('product-photos');
 | 
						|
 | 
						|
        $cntAfter = $this->ProductPhotos->find()->count();
 | 
						|
        $this->assertEquals($cntBefore - 1, $cntAfter);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Test image method
 | 
						|
     *
 | 
						|
     * Tests the image action with a logged in user
 | 
						|
     *
 | 
						|
     * @return void
 | 
						|
     * @throws Exception
 | 
						|
     *
 | 
						|
     * @uses ProductPhotosController::image
 | 
						|
     */
 | 
						|
    public function testImageGetLoggedIn(): void
 | 
						|
    {
 | 
						|
        $id = '2c386086-f4c5-4093-bea5-ee9c29479f58';
 | 
						|
        // $this->loginUserByRole('admin');
 | 
						|
        $url = [
 | 
						|
            'plugin' => 'CakeProducts',
 | 
						|
            'controller' => 'ProductPhotos',
 | 
						|
            'action' => 'image',
 | 
						|
            $id,
 | 
						|
        ];
 | 
						|
        $this->get($url);
 | 
						|
        $this->assertResponseCode(200);
 | 
						|
    }
 | 
						|
}
 |