Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests #89

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions tests/cron_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
*
* phpBB Media Embed PlugIn extension for the phpBB Forum Software package.
*
* @copyright (c) 2022 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace phpbb\mediaembed\tests;

class cron_test extends \phpbb_test_case
{
/** @var \phpbb\config\config */
protected $config;

/** @var \phpbb\mediaembed\cron\purge_cache */
protected $cron_task;

/** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\mediaembed\cache\cache */
protected $cache;

protected function setUp(): void
{
parent::setUp();

$this->config = new \phpbb\config\config([]);
$this->cache = $this->getMockBuilder('\phpbb\mediaembed\cache\cache')
->disableOriginalConstructor()
->getMock();

$this->cron_task = new \phpbb\mediaembed\cron\purge_cache($this->config, $this->cache);
}

/**
* Test the cron task runs correctly
*/
public function test_run()
{
// Get the last_run
$last_run = $this->config['mediaembed_last_gc'];

// Test purge_cache() is called only once
$this->cache->expects(self::once())
->method('purge_mediaembed_cache');

// Run the cron task
$this->cron_task->run();

// Assert the autogroups_last_run value has been updated
self::assertNotEquals($last_run, $this->config['mediaembed_last_gc']);
}

/**
* Data set for test_should_run
*
* @return array Array of test data
*/
public function should_run_data()
{
return [
[time(), false],
[strtotime('23 hours ago'), false],
[strtotime('25 hours ago'), true],
['', true],
[0, true],
[null, true],
];
}

/**
* Test cron task should run after 24 hours
*
* @dataProvider should_run_data
*/
public function test_should_run($time, $expected)
{
// Set the last cron run time
$this->config['mediaembed_last_gc'] = $time;

// Assert we get the expected result from should_run()
self::assertSame($expected, $this->cron_task->should_run());
}

public function is_runnable_data()
{
return [
[true],
[false],
];
}

/**
* Test the cron task is runnable
*
* @dataProvider is_runnable_data
*/
public function test_is_runnable($expected)
{
$this->config['media_embed_enable_cache'] = $expected;

self::assertSame($expected, $this->cron_task->is_runnable());
}
}
55 changes: 55 additions & 0 deletions tests/custom_sites_collection_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace phpbb\mediaembed\tests;

use phpbb\extension\manager;
use phpbb\filesystem\filesystem;
use phpbb\mediaembed\collection\customsitescollection;
use PHPUnit\Framework\MockObject\MockObject;

class custom_sites_collection_test extends \phpbb_test_case
{
/** @var manager|MockObject */
protected $ext_manager;
protected $phpbb_root_path;
protected $php_ext;

protected function setUp(): void
{
parent::setUp();

global $phpbb_root_path, $phpEx;

$this->ext_manager = $this->getMockBuilder('\phpbb\extension\manager')
->disableOriginalConstructor()
->getMock();

$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $phpEx;
}

public function test_get_collection()
{
$finder = new \phpbb\finder(
new filesystem(),
$this->phpbb_root_path,
$this->getMockBuilder('\phpbb\cache\service')->disableOriginalConstructor()->getMock(),
$this->php_ext,
'_ext_finder'
);

$this->ext_manager->expects(self::once())
->method('get_finder')
->willReturn($finder);

$customsitescollection = new customsitescollection($this->ext_manager);

$collection = $customsitescollection->get_collection();

$this->assertContains('phpBB/ext/phpbb/mediaembed/collection/sites/codepen.yml', $collection);
$this->assertContains('phpBB/ext/phpbb/mediaembed/collection/sites/snotr.yml', $collection);
$this->assertContains('phpBB/ext/phpbb/mediaembed/collection/sites/ok.yml', $collection);
$this->assertContains('phpBB/ext/phpbb/mediaembed/collection/sites/videopress.yml', $collection);
$this->assertNotContains('phpBB/ext/phpbb/mediaembed/collection/sites/youtube.yml', $collection);
}
}