diff --git a/addons/Core/Core.inc b/addons/Core/Core.inc index f0a95aa..c9b1dd6 100644 --- a/addons/Core/Core.inc +++ b/addons/Core/Core.inc @@ -442,6 +442,20 @@ class ComicPressAddonCore extends ComicPressAddon { $this->comicpress->init(); } } + + function _glob($pattern) { return glob($pattern); } + function _file_get_contents($file) { return file_get_contents($file); } + + function get_layout_choices() { + $layouts = array(); + foreach ($this->_glob(get_template_directory() . '/layouts/*') as $file) { + $content = $this->_file_get_contents($file); + if (preg_match('#/\*.*Layout Name: ([^\n]+).*\*/#s', $content, $matches) > 0) { + $layouts[pathinfo($file, PATHINFO_BASENAME)] = $matches[1]; + } + } + return $layouts; + } } ?> \ No newline at end of file diff --git a/addons/Core/test/OptionsPageTest.php b/addons/Core/test/OptionsPageTest.php index 259d751..1447bcd 100644 --- a/addons/Core/test/OptionsPageTest.php +++ b/addons/Core/test/OptionsPageTest.php @@ -383,7 +383,53 @@ class CoreTest extends PHPUnit_Framework_TestCase { $this->assertEquals($value, $post->{$key}); } } - } + } + } + + function providerTestGetLayoutChoices() { + return array( + array( + array(), + array() + ), + array( + array( + 'layout.php' => << << 'Test') + ), + ); + } + + /** + * @dataProvider providerTestGetLayoutChoices + */ + function testGetLayoutChoices($files, $expected_results) { + $core = $this->getMock('ComicPressAddonCore', array('_glob', '_file_get_contents')); + + _set_template_directory('/test'); + + $file_names = array(); + foreach (array_keys($files) as $file) { $file_names[] = '/test/layouts/' . $file; } + + $core->expects($this->once())->method('_glob')->with('/test/layouts/*')->will($this->returnValue($file_names)); + foreach ($files as $file => $contents) { + $core->expects($this->once())->method('_file_get_contents')->with('/test/layouts/' . $file)->will($this->returnValue($contents)); + } + + $this->assertEquals($expected_results, $core->get_layout_choices()); } }