summaryrefslogtreecommitdiff
path: root/tests/unit/Web/TAssetManagerTest.php
blob: f54858225d9dcd1dbf2483d2d0b5045f7e24f886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
require_once dirname(__FILE__).'/../phpunit.php';

Prado::using('System.Web.TAssetManager');

/**
 * @package System.Web
 */
class TAssetManagerTest extends PHPUnit_Framework_TestCase {

	public static $app = null;
	public static $assetDir = null;

	public function setUp () {
		// Fake environment variables needed to determine path
		$_SERVER['HTTP_HOST'] = 'localhost';
		$_SERVER['SERVER_NAME'] = 'localhost';
		$_SERVER['SERVER_PORT'] = '80';
		$_SERVER['REQUEST_METHOD'] = 'GET';
		$_SERVER['REQUEST_URI'] = '/demos/personal/index.php?page=Links';
		$_SERVER['SCRIPT_NAME'] = '/demos/personal/index.php';
		$_SERVER['PHP_SELF'] = '/demos/personal/index.php';
		$_SERVER['QUERY_STRING'] = 'page=Links';
		$_SERVER['SCRIPT_FILENAME'] = __FILE__;
		$_SERVER['PATH_INFO'] = __FILE__;
		$_SERVER['HTTP_REFERER'] = 'http://www.pradosoft.com';
		$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3';
		$_SERVER['REMOTE_HOST'] = 'localhost';
		
		if (self::$app===null) {
			self::$app=new TApplication(dirname(__FILE__).'/app');
		}
		
		if (self::$assetDir===null) self::$assetDir= dirname(__FILE__).'/assets';
		// Make asset directory if not exists 
	    if (!file_exists (self::$assetDir)) {
    		if (is_writable(dirname(self::$assetDir))) 
    		  mkdir (self::$assetDir) ;
    		else 
    		  throw new Exception ('Directory '.dirname(self::$assetDir).' is not writable');
	    } elseif (!is_dir (self::$assetDir)) {
	       throw new Exception (self::$assetDir.' exists and is not a directory');
	    }
		// Define an alias to asset directory
		prado::setPathofAlias('AssetAlias', self::$assetDir);
		
	}
	
	private function removeDirectory ($dir) {
	  // Let's be sure $dir is a directory to avoir any error. Clear the cache !
	  clearstatcache();
	  if (is_dir($dir)) {
	    foreach (scandir($dir) as $content) {
	      if ($content==='.' or $content==='..') continue; // skip . and ..
	      $content=$dir.'/'.$content;
	      if (is_dir($content)) 
	        $this->removeDirectory ($content); // Recursivly remove directories
	      else
	        unlink ($content); // Remove file
	    }
	    // Now, directory should be empty, remove it
	    rmdir ($dir);
	  }
	}
	
	public function tearDown () {
		// Make some cleaning :)
	    $this->removeDirectory(self::$assetDir); 
	}
	
	public function testInit() {
		
		$manager=new TAssetManager ();

		$manager->init (null);
		
		self::assertEquals(self::$assetDir, $manager->getBasePath());
		self::assertEquals($manager, self::$app->getAssetManager());
		
		// No, remove asset directory, and catch the exception
		if (is_dir(self::$assetDir)) $this->removeDirectory (self::$assetDir);
		try {
			$manager->init (null);
			self::fail ('Expected TConfigurationException not thrown');
		} catch (TConfigurationException $e) {}
	}

	public function testSetBasePath() {
		$manager = new TAssetManager ();
		// First try, invalid directory
		try {
			$manager->setBasePath('invalid');
			self::fail('Expected TInvalidDataValueException not thrown');
		} catch (TInvalidDataValueException $e) {}
		
		// Next, standard asset directory, should work
		
		$manager->setBasePath ('AssetAlias');
		self::assertEquals(self::$assetDir, $manager->getBasePath());
		
		// Finally, test to change after init
		$manager->init (null);
		try {
			$manager->setBasePath ('test');
			self::fail ('Expected TInvalidOperationException not thrown');
		} catch (TInvalidOperationException $e) {}

	}

	public function testSetBaseUrl() {
		$manager=new TAssetManager ();
		$manager->setBaseUrl ('/assets/');
		self::assertEquals("/assets", $manager->getBaseUrl());
	
		$manager->init (null);
		try {
			$manager->setBaseUrl ('/test');
			self::fail ('Expected TInvalidOperationException not thrown');
		} catch (TInvalidOperationException $e) {}
		
	}

	public function testPublishFilePath() {
		$manager=new TAssetManager();
		$manager->setBaseUrl('/');
		$manager->init (null);
		
		// Try to publish a single file
	    $fileToPublish=dirname(__FILE__).'/data/pradoheader.gif';
		$publishedUrl = $manager->publishFilePath($fileToPublish);
		$publishedFile=self::$assetDir.$publishedUrl;
		self::assertEquals($publishedFile, $manager->getPublishedPath($fileToPublish));
		self::assertEquals($publishedUrl, $manager->getPublishedUrl($fileToPublish));
		self::assertTrue(is_file($publishedFile));
		
	    //  try to publish invalid file
	    try {
	      $manager->publishFilePath('invalid_file');
	      self::fail('Expected TInvalidDataValueException not thrown');
	    } catch (TInvalidDataValueException $e) {}
	}
	
	public function testPublishFilePathWithDirectory () {
	    $manager=new TAssetManager();
		$manager->setBaseUrl('/');
		$manager->init (null);
		
		// Try to publish a directory
	    $dirToPublish=dirname(__FILE__).'/data';
		$publishedUrl = $manager->publishFilePath($dirToPublish);
		$publishedDir=self::$assetDir.$publishedUrl;
		self::assertEquals($publishedDir, $manager->getPublishedPath($dirToPublish));
		self::assertEquals($publishedUrl, $manager->getPublishedUrl($dirToPublish));
		self::assertTrue(is_dir($publishedDir));
		self::assertTrue(is_file($publishedDir.'/pradoheader.gif'));
		
	}

	public function testPublishTarFile() {
		$manager=new TAssetManager();
		$manager->setBaseUrl('/');
		$manager->init (null);
		
		$tarFile=dirname(__FILE__).'/data/aTarFile.tar';
		$md5File=dirname(__FILE__).'/data/aTarFile.md5';
		
		// First, try with bad md5
	    try {
	      $manager->publishTarFile($tarFile, 'badMd5File');
	      self::fail('Expected TInvalidDataValueException not thrown');
	    } catch (TInvalidDataValueException $e) {}
	    
	    // Then, try with real md5 file
	    $publishedUrl=$manager->publishTarFile($tarFile, $md5File);
	    $publishedDir=self::$assetDir.$publishedUrl;
	    self::assertTrue(is_dir($publishedDir));
	    self::assertTrue(is_file($publishedDir.'/pradoheader.gif'));
	    self::assertTrue(is_file($publishedDir.'/aTarFile.md5'));

	}
}
?>