LocationViewTest.php 4.96 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */

namespace Temando\Shipping\ViewModel\Location;

use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Request;
use Temando\Shipping\Model\Location;
use Temando\Shipping\Model\LocationInterface;
use Temando\Shipping\ViewModel\DataProvider\EntityUrlInterface;
use Temando\Shipping\ViewModel\PageActionsInterface;
use Temando\Shipping\ViewModel\ShippingApiInterface;

/**
 * Temando Location View Model Test
 *
 * @package  Temando\Shipping\Test\Integration
 * @author   Christoph Aßmann <christoph.assmann@netresearch.de>
 * @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link     http://www.temando.com/
 */
class LocationViewTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @return LocationInterface
     */
    private function getLocation()
    {
        $location = Bootstrap::getObjectManager()->create(Location::class, ['data' => [
            LocationInterface::NAME => 'Foo Location',
            LocationInterface::LOCATION_ID => '00000000-6000-0006-0000-000000000000',
        ]]);

        return $location;
    }

    /**
     * @test
     */
    public function backButtonIsAvailableInEditComponents()
    {
        /** @var LocationEdit $locationEdit */
        $locationEdit = Bootstrap::getObjectManager()->get(LocationEdit::class);
        $this->assertInstanceOf(PageActionsInterface::class, $locationEdit);

        $actions = $locationEdit->getMainActions();

        $this->assertNotEmpty($actions);
        $this->assertInternalType('array', $actions);
        $this->assertArrayHasKey('back', $actions);
    }

    /**
     * @test
     * @magentoConfigFixture default/carriers/temando/session_endpoint https://auth.temando.io/v1/
     * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
     */
    public function shippingApiCredentialsAreAvailableInLocationComponents()
    {
        /** @var LocationEdit $locationEdit */
        $locationEdit = Bootstrap::getObjectManager()->get(LocationEdit::class);
        $this->assertInstanceOf(ShippingApiInterface::class, $locationEdit);
        $this->assertEquals('https://foo.temando.io/v1/', $locationEdit->getShippingApiAccess()->getApiEndpoint());
    }

    /**
     * @test
     */
    public function locationIdIsAvailableInEditComponent()
    {
        $location = $this->getLocation();

        $request = $this->getMockBuilder(Request::class)
            ->setMethods(['getParam'])
            ->disableOriginalConstructor()
            ->getMock();
        $request
            ->expects($this->exactly(1))
            ->method('getParam')
            ->with(LocationInterface::LOCATION_ID)
            ->willReturn($location->getLocationId());

        /** @var LocationEdit $locationEdit */
        $locationEdit = Bootstrap::getObjectManager()->create(LocationEdit::class, [
            'request' => $request,
        ]);
        $this->assertEquals($location->getLocationId(), $locationEdit->getLocationId());
    }

    /**
     * @test
     */
    public function entityUrlsAreAvailableInLocationComponents()
    {
        /** @var Location $location */
        $location = $this->getLocation();

        /** @var LocationEdit $locationEdit */
        $locationEdit = Bootstrap::getObjectManager()->get(LocationEdit::class);
        $this->assertInstanceOf(EntityUrlInterface::class, $locationEdit->getLocationUrl());

        // application does not provide view action for locations
        $this->assertEmpty($locationEdit->getLocationUrl()->getViewActionUrl($location->getData()));
        $this->assertContains('new', $locationEdit->getLocationUrl()->getNewActionUrl());
        $this->assertContains('index', $locationEdit->getLocationUrl()->getListActionUrl());

        $editUrl = $locationEdit->getLocationUrl()->getEditActionUrl($location->getData());
        $this->assertContains('edit', $editUrl);
        $this->assertContains($location->getLocationId(), $editUrl);

        $deleteUrl = $locationEdit->getLocationUrl()->getDeleteActionUrl($location->getData());
        $this->assertContains('delete', $deleteUrl);
        $this->assertContains($location->getLocationId(), $deleteUrl);
    }

    /**
     * @test
     */
    public function maliciousParamValuesGetStripped()
    {
        $badLocationId = '<script>alert("location");</script>';

        $request = $this->getMockBuilder(Request::class)
            ->setMethods(['getParam'])
            ->disableOriginalConstructor()
            ->getMock();
        $request
            ->expects($this->exactly(1))
            ->method('getParam')
            ->with(LocationInterface::LOCATION_ID)
            ->willReturn($badLocationId);

        /** @var LocationEdit $locationEdit */
        $locationEdit = Bootstrap::getObjectManager()->create(LocationEdit::class, [
            'request' => $request,
        ]);
        $this->assertRegExp('/^[\w0-9-_]+$/', $locationEdit->getLocationId());
    }
}