Skip to content

Commit

Permalink
test: import sitemap tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanorosanelli committed May 10, 2024
1 parent 7103246 commit 2ba37a1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
91 changes: 91 additions & 0 deletions tests/TestCase/Command/ImportSitemapCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);

/**
* BEdita Brevia plugin
*
* Copyright 2023 Atlas Srl
*/
namespace Brevia\BEdita\Test\TestCase\Command;

use BEdita\Core\Model\Entity\ObjectEntity;
use Brevia\BEdita\Test\TestMockTrait;
use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;

/**
* {@see \Brevia\BEdita\Command\ImportSitemapCommand} Test Case
*
* @coversDefaultClass \Brevia\BEdita\Command\ImportSitemapCommand
*/
class ImportSitemapCommandTest extends TestCase
{
use ConsoleIntegrationTestTrait;
use TestMockTrait;

/**
* @inheritDoc
*/
public function setUp(): void
{
parent::setUp();
$this->useCommandRunner();
Router::reload();
}

/**
* Test buildOptionParser method
*
* @return void
* @covers ::buildOptionParser()
*/
public function testBuildOptionParser(): void
{
$this->exec('import_sitemap --help');
$this->assertOutputContains('File path or URL of sitemap to import');
$this->assertOutputContains('Optional path prefix of URLs to import');
$this->assertOutputContains('Collection used to index');
}

/**
* Test options failure
*
* @return void
* @covers ::initialize()
* @covers ::execute()
*/
public function testOptionFailure(): void
{
$this->exec('import_sitemap --sitemap /not/existing/path --collection gustavo');
$this->assertExitError('File not found: /not/existing/path');

$this->mockClientResponse(json_encode([]));
$xmlPath = sprintf('%s/tests/files/sitemap.xml', getcwd());
$this->exec(sprintf('import_sitemap --sitemap %s --collection gustavo', $xmlPath));
$this->assertExitError('Collection not found: gustavo');

$this->mockClientResponse('[{"cmetadata": {"id":"1"}}]');
$this->mockTable('Collections', new ObjectEntity());
$xmlPath = sprintf('%s/tests/files/empty.csv', getcwd());
$this->exec(sprintf('import_sitemap --sitemap %s --collection gustavo', $xmlPath));
$this->assertExitError('No URLs found in sitemap');

}

/**
* Test command success
*
* @return void
* @covers ::execute()
*/
public function testCommand(): void
{
$this->mockTable('Collections', new ObjectEntity());
$this->mockTable('Links', new ObjectEntity());
$this->mockClientResponse('[{"cmetadata": {"id":"1"}}]', 200, 3);
$xmlPath = sprintf('%s/tests/files/sitemap.xml', getcwd());
$this->exec(sprintf('import_sitemap --sitemap %s --collection gustavo', $xmlPath));
$this->assertExitSuccess('Done');
}
}
14 changes: 14 additions & 0 deletions tests/files/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>https://example.com/home</loc>
<lastmod>2024-04-28T08:02:38+00:00</lastmod>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://example.com/page2</loc>
<lastmod>2024-04-28T08:02:38+00:00</lastmod>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>
</urlset>

0 comments on commit 2ba37a1

Please sign in to comment.