ViewsServiceTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Elgg;
  3. class ViewsServiceTest extends \PHPUnit_Framework_TestCase {
  4. /**
  5. * @var \Elgg\PluginHooksService
  6. */
  7. protected $hooks;
  8. /**
  9. * @var \Elgg\ViewsService
  10. */
  11. protected $views;
  12. public function setUp() {
  13. $this->viewsDir = dirname(dirname(__FILE__)) . "/test_files/views";
  14. $this->hooks = new \Elgg\PluginHooksService();
  15. $this->logger = $this->getMock('\Elgg\Logger', array(), array(), '', false);
  16. $this->views = new \Elgg\ViewsService($this->hooks, $this->logger);
  17. $this->views->autoregisterViews('', "$this->viewsDir/default", "$this->viewsDir/", 'default');
  18. // supports deprecation wrapper for $vars['user']
  19. _elgg_services()->setValue('session', \ElggSession::getMock());
  20. }
  21. public function testCanExtendViews() {
  22. $this->views->extendView('foo', 'bar');
  23. // Unextending valid extension succeeds.
  24. $this->assertTrue($this->views->unextendView('foo', 'bar'));
  25. // Unextending non-existent extension "fails."
  26. $this->assertFalse($this->views->unextendView('foo', 'bar'));
  27. }
  28. public function testViewCanOnlyExistIfString() {
  29. $this->assertFalse($this->views->viewExists(1));
  30. $this->assertFalse($this->views->viewExists(new \stdClass));
  31. }
  32. public function testRegistersPhpFilesAsViews() {
  33. $this->assertTrue($this->views->viewExists('js/interpreted.js'));
  34. }
  35. public function testRegistersStaticFilesAsViews() {
  36. $this->assertTrue($this->views->viewExists('js/static.js'));
  37. }
  38. public function testUsesPhpToRenderNonStaticViews() {
  39. $this->assertEquals("// PHPin", $this->views->renderView('js/interpreted.js', array(
  40. 'in' => 'in',
  41. )));
  42. }
  43. public function testDoesNotUsePhpToRenderStaticViews() {
  44. $expected = file_get_contents("$this->viewsDir/default/js/static.js");
  45. $this->assertEquals($expected, $this->views->renderView('js/static.js'));
  46. }
  47. public function testStoresDirectoryForViewLocation() {
  48. $this->assertEquals("$this->viewsDir/", $this->views->getViewLocation('js/interpreted.js', 'default'));
  49. }
  50. public function testViewtypesCanFallBack() {
  51. $this->views->registerViewtypeFallback('mobile');
  52. $this->assertTrue($this->views->doesViewtypeFallBack('mobile'));
  53. }
  54. public function testViewsCanExistBasedOnViewtypeFallback() {
  55. $this->views->registerViewtypeFallback('mobile');
  56. $this->assertTrue($this->views->viewExists('js/interpreted.js', 'mobile'));
  57. $this->assertEquals('// PHP', $this->views->renderView('js/interpreted.js', array(), false, 'mobile'));
  58. }
  59. public function testCanRegisterViewsAsCacheable() {
  60. $this->assertFalse($this->views->isCacheableView('js/interpreted.js'));
  61. $this->views->registerCacheableView('js/interpreted.js');
  62. $this->assertTrue($this->views->isCacheableView('js/interpreted.js'));
  63. }
  64. public function testStaticViewsAreAlwaysCacheable() {
  65. $this->assertTrue($this->views->isCacheableView('js/static.js'));
  66. }
  67. public function testCanAlterViewInput() {
  68. $this->hooks->registerHandler('view_vars', 'js/interpreted.js', function ($h, $t, $v, $p) {
  69. $v['in'] = 'out';
  70. return $v;
  71. });
  72. $this->assertEquals("// PHPout", $this->views->renderView('js/interpreted.js'));
  73. }
  74. public function testCanAlterViewOutput() {
  75. $this->hooks->registerHandler('view', 'js/interpreted.js', function ($h, $t, $v, $p) {
  76. return '// Hello';
  77. });
  78. $this->assertEquals("// Hello", $this->views->renderView('js/interpreted.js'));
  79. }
  80. }