CmsBlockTest.php 4.22 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\GraphQl\Cms;

use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Widget\Model\Template\FilterEmulate;

class CmsBlockTest extends GraphQlAbstract
{
    /**
     * @var BlockRepositoryInterface
     */
    private $blockRepository;

    /**
     * @var FilterEmulate
     */
    private $filterEmulate;

    protected function setUp()
    {
        $this->blockRepository = Bootstrap::getObjectManager()->get(BlockRepositoryInterface::class);
        $this->filterEmulate = Bootstrap::getObjectManager()->get(FilterEmulate::class);
    }

    /**
     * Verify the fields of CMS Block selected by identifiers
     *
     * @magentoApiDataFixture Magento/Cms/_files/blocks.php
     */
    public function testGetCmsBlock()
    {
        $cmsBlock = $this->blockRepository->getById('enabled_block');
        $cmsBlockData = $cmsBlock->getData();
        $renderedContent = $this->filterEmulate->setUseSessionInUrl(false)->filter($cmsBlock->getContent());

        $query =
            <<<QUERY
{
  cmsBlocks(identifiers: "enabled_block") {
    items {
      identifier
      title
      content
    }
  }
}
QUERY;
        $response = $this->graphQlQuery($query);

        self::assertArrayHasKey('cmsBlocks', $response);
        self::assertArrayHasKey('items', $response['cmsBlocks']);

        self::assertEquals($cmsBlockData['identifier'], $response['cmsBlocks']['items'][0]['identifier']);
        self::assertEquals($cmsBlockData['title'], $response['cmsBlocks']['items'][0]['title']);
        self::assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']);
    }

    /**
     * Verify the message when CMS Block is disabled
     *
     * @expectedException \Exception
     * @expectedExceptionMessage The CMS block with the "disabled_block" ID doesn't exist
     *
     * @magentoApiDataFixture Magento/Cms/_files/blocks.php
     */
    public function testGetDisabledCmsBlock()
    {
        $query =
            <<<QUERY
{
  cmsBlocks(identifiers: "disabled_block") {
    items {
      identifier
      title
      content
    }
  }
}
QUERY;
        $this->graphQlQuery($query);
    }

    /**
     * Verify the message when identifiers were not specified
     *
     * @expectedException \Exception
     * @expectedExceptionMessage "identifiers" of CMS blocks should be specified
     */
    public function testGetCmsBlocksWithoutIdentifiers()
    {
        $query =
            <<<QUERY
{
  cmsBlocks(identifiers: []) {
    items {
      identifier
      title
      content
    }
  }
}
QUERY;
        $this->graphQlQuery($query);
    }

    /**
     * Verify the message when CMS Block with such identifiers does not exist
     *
     * @expectedException \Exception
     * @expectedExceptionMessage The CMS block with the "nonexistent_id" ID doesn't exist.
     */
    public function testGetCmsBlockByNonExistentIdentifier()
    {
        $query =
            <<<QUERY
{
  cmsBlocks(identifiers: "nonexistent_id") {
    items {
      identifier
      title
      content
    }
  }
}
QUERY;
        $this->graphQlQuery($query);
    }

    /**
     * Verify the fields of CMS Block selected by identifiers
     *
     * @magentoApiDataFixture Magento/Cms/_files/blocks.php
     */
    public function testGetEnabledAndDisabledCmsBlockInOneRequest()
    {
        $query =
            <<<QUERY
{
  cmsBlocks(identifiers: ["enabled_block", "disabled_block"]) {
    items {
      identifier
    }
  }
}
QUERY;

        try {
            $this->graphQlQuery($query);
            self::fail('Response should contains errors.');
        } catch (ResponseContainsErrorsException $e) {
            $responseData = $e->getResponseData();
        }

        self::assertNotEmpty($responseData);
        self::assertEquals('enabled_block', $responseData['data']['cmsBlocks']['items'][0]['identifier']);
        self::assertEquals(
            'The CMS block with the "disabled_block" ID doesn\'t exist.',
            $responseData['errors'][0]['message']
        );
    }
}