Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomano committed Jan 2, 2025
1 parent 57828e7 commit 127445b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
31 changes: 28 additions & 3 deletions tests/Services/RobotsGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,33 @@

beforeEach(function () {
$this->generator = new RobotsGenerator();

$this->tempDir = sys_get_temp_dir() . '/sitemaps';
mkdir($this->tempDir, 0777, true);

file_put_contents($this->tempDir . '/sitemap.xml', '');
file_put_contents($this->tempDir . '/sitemap.xml.gz', '');

Config::$boarddir = $this->tempDir;
});

it('generator method', function () {
afterEach(function () {
if (is_dir($this->tempDir)) {
array_map('unlink', glob($this->tempDir . '/*'));
rmdir($this->tempDir);
}
});

it('generates correct links', function () {
$this->generator->generate();

Config::$modSettings['xmlnews_enable'] = true;

expect(Utils::$context['new_robots_content'])
->toContain('User-agent: *');
});

it('generator method with SEF enabled', function () {
it('generates correct links with SEF enabled', function () {
$this->generator->useSef = true;
$this->generator->generate();

Expand All @@ -25,7 +42,7 @@
$this->generator->useSef = false;
});

it('generator method with queryless_urls enabled', function () {
it('generates correct links with queryless_urls enabled', function () {
Config::$modSettings['queryless_urls'] = true;

$this->generator->generate();
Expand All @@ -35,3 +52,11 @@

Config::$modSettings['queryless_urls'] = false;
});

it('can process custom rules', function () {
$this->generator->customRules['GoogleBot']['disallow'][] = '/';
$this->generator->generate();

expect(Utils::$context['new_robots_content'])
->toContain('User-agent: GoogleBot');
});
17 changes: 17 additions & 0 deletions tests/Services/SitemapGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,20 @@ function(AddonEvent $event) use ($testAddon) {
->toContain('<loc>https://example.com/custom</loc>')
->and(file_exists($this->tempDir . '/sitemap.xml'))->toBeTrue();
});

it('gets last date from links array', function () {
$maxDate = time();
$links = [
['loc' => 'https://example.com/page1', 'lastmod' => strtotime('-3 days', $maxDate)],
['loc' => 'https://example.com/page2', 'lastmod' => $maxDate]
];

$method = new ReflectionMethod($this->generator, 'getLastDate');
$result = $method->invoke($this->generator, $links);

expect($result)->toBe($maxDate);

$result = $method->invoke($this->generator, []);

expect($result)->toBe($maxDate);
});

0 comments on commit 127445b

Please sign in to comment.