Browse Source

Merge branch 'githubjeka-patch-3'

tags/2.0.10
SilverFire - Dmitry Naumenko 8 years ago
parent
commit
dc23a6d3d8
  1. 1
      framework/CHANGELOG.md
  2. 44
      framework/web/View.php
  3. 1
      tests/data/web/assetSources/css/stub.css
  4. 100
      tests/framework/web/AssetBundleTest.php

1
framework/CHANGELOG.md

@ -5,6 +5,7 @@ Yii Framework 2 Change Log
------------------------
- Bug #9027: Fixed descendant class of `yii\web\UploadedFile` returns parent instances in case invoked after it (andrewnester)
- Bug #9101: Fixed `yii\web\View` to respect `yii\web\AssetManager::appendTimstamp` property (githubjeka, silverfire)
- Bug #12428: Fixed `yii\db\mysql\QueryBuilder` causes warning when insert default rows into a table without primary key (DrmagicE)
- Enh #6996: Added `yii\web\MultipartFormDataParser`, which allows proper processing of 'multipart/form-data' encoded non POST requests (klimov-paul)
- Enh #9989: ActiveForm now respects formtarget, formmethod and formenctype attributes of submit button (AnatolyRugalev)

44
framework/web/View.php

@ -398,17 +398,27 @@ class View extends \yii\base\View
{
$url = Yii::getAlias($url);
$key = $key ?: $url;
$depends = ArrayHelper::remove($options, 'depends', []);
if (empty($depends)) {
$this->cssFiles[$key] = Html::cssFile($url, $options);
} else {
$this->getAssetManager()->bundles[$key] = new AssetBundle([
'baseUrl' => '',
'css' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')],
$url = str_replace(Yii::getAlias('@web'), '', $url);
$url = strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/');
/** @var AssetBundle $bundle */
$bundle = Yii::createObject([
'class' => AssetBundle::className(),
'baseUrl' => '@web',
'basePath' => '@webroot',
'css' => (array)$url,
'cssOptions' => $options,
'depends' => (array)$depends,
]);
if (empty($depends)) {
$url = $this->getAssetManager()->getAssetUrl($bundle, $url);
$this->cssFiles[$key] = Html::cssFile($url, $options);
} else {
$this->getAssetManager()->bundles[$key] = $bundle;
$this->registerAssetBundle($key);
}
}
@ -463,18 +473,28 @@ class View extends \yii\base\View
{
$url = Yii::getAlias($url);
$key = $key ?: $url;
$depends = ArrayHelper::remove($options, 'depends', []);
$url = str_replace(Yii::getAlias('@web'), '', $url);
$url = strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/');
/** @var AssetBundle $bundle */
$bundle = Yii::createObject([
'class' => AssetBundle::className(),
'baseUrl' => '@web',
'basePath' => '@webroot',
'css' => (array)$url,
'cssOptions' => $options,
'depends' => (array)$depends,
]);
if (empty($depends)) {
$url = $this->getAssetManager()->getAssetUrl($bundle, $url);
$position = ArrayHelper::remove($options, 'position', self::POS_END);
$this->jsFiles[$position][$key] = Html::jsFile($url, $options);
} else {
$this->getAssetManager()->bundles[$key] = new AssetBundle([
'baseUrl' => '',
'js' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')],
'jsOptions' => $options,
'depends' => (array) $depends,
]);
$this->getAssetManager()->bundles[$key] = $bundle;
$this->registerAssetBundle($key);
}
}

1
tests/data/web/assetSources/css/stub.css

@ -0,0 +1 @@
/* dumb CSS file */

100
tests/framework/web/AssetBundleTest.php

@ -22,11 +22,11 @@ class AssetBundleTest extends \yiiunit\TestCase
parent::setUp();
$this->mockApplication();
Yii::setAlias('@testWeb', '/');
Yii::setAlias('@testWebRoot', '@yiiunit/data/web');
Yii::setAlias('@testAssetsPath', '@testWebRoot/assets');
Yii::setAlias('@testAssetsUrl', '@testWeb/assets');
Yii::setAlias('@testSourcePath', '@testWebRoot/assetSources');
Yii::setAlias('@web', '/');
Yii::setAlias('@webroot', '@yiiunit/data/web');
Yii::setAlias('@testAssetsPath', '@webroot/assets');
Yii::setAlias('@testAssetsUrl', '@web/assets');
Yii::setAlias('@testSourcePath', '@webroot/assetSources');
}
/**
@ -57,16 +57,22 @@ class AssetBundleTest extends \yiiunit\TestCase
$bundle->publish($am);
$this->assertTrue(is_dir($bundle->basePath));
foreach ($bundle->js as $filename) {
$this->sourcesPublish_VerifyFiles('css', $bundle);
$this->sourcesPublish_VerifyFiles('js', $bundle);
$this->assertTrue(rmdir($bundle->basePath));
}
private function sourcesPublish_VerifyFiles($type, $bundle)
{
foreach ($bundle->$type as $filename) {
$publishedFile = $bundle->basePath . DIRECTORY_SEPARATOR . $filename;
$sourceFile = $bundle->sourcePath . DIRECTORY_SEPARATOR . $filename;
$this->assertFileExists($publishedFile);
$this->assertFileEquals($publishedFile, $sourceFile);
$this->assertTrue(unlink($publishedFile));
}
$this->assertTrue(rmdir($bundle->basePath . DIRECTORY_SEPARATOR . 'js'));
$this->assertTrue(rmdir($bundle->basePath));
$this->assertTrue(rmdir($bundle->basePath . DIRECTORY_SEPARATOR . $type));
}
public function testSourcesPublishedBySymlink()
@ -336,12 +342,57 @@ EOF;
EOF;
$this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
}
public function registerFileDataProvider()
{
return [
[
'js', '@web/assetSources/js/jquery.js', true,
'123<script src="/assetSources/js/jquery.js?v=1454496648"></script>4',
],
[
'js', '@web/assetSources/js/missing-file.js', true,
'123<script src="/assetSources/js/missing-file.js"></script>4',
],
[
'js', '@web/assetSources/js/jquery.js', false,
'123<script src="/assetSources/js/jquery.js"></script>4',
],
[
'css', '@web/assetSources/css/stub.css', true,
'1<link href="/assetSources/css/stub.css?v=1473509579" rel="stylesheet">234',
],
[
'css', '@web/assetSources/css/missing-file.css', true,
'1<link href="/assetSources/css/missing-file.css" rel="stylesheet">234',
],
[
'css', '@web/assetSources/css/stub.css', false,
'1<link href="/assetSources/css/stub.css" rel="stylesheet">234',
],
];
}
/**
* @dataProvider registerFileDataProvider
* @param string $type either `js` or `css`
* @param $path
* @param bool $appendTimestamp
* @param $expected
*/
public function testRegisterFileAppendTimestamp($type, $path, $appendTimestamp, $expected)
{
$view = $this->getView(['appendTimestamp' => $appendTimestamp]);
$method = 'register' . ucfirst($type) . 'File';
$view->$method($path);
$this->assertEquals($expected, $view->renderFile('@yiiunit/data/views/rawlayout.php'));
}
}
class TestSimpleAsset extends AssetBundle
{
public $basePath = '@testWebRoot/js';
public $baseUrl = '@testWeb/js';
public $basePath = '@webroot/js';
public $baseUrl = '@web/js';
public $js = [
'jquery.js',
];
@ -353,12 +404,15 @@ class TestSourceAsset extends AssetBundle
public $js = [
'js/jquery.js',
];
public $css = [
'css/stub.css',
];
}
class TestAssetBundle extends AssetBundle
{
public $basePath = '@testWebRoot/files';
public $baseUrl = '@testWeb/files';
public $basePath = '@webroot/files';
public $baseUrl = '@web/files';
public $css = [
'cssFile.css',
];
@ -372,8 +426,8 @@ class TestAssetBundle extends AssetBundle
class TestJqueryAsset extends AssetBundle
{
public $basePath = '@testWebRoot/js';
public $baseUrl = '@testWeb/js';
public $basePath = '@webroot/js';
public $baseUrl = '@web/js';
public $js = [
'jquery.js',
];
@ -384,14 +438,14 @@ class TestJqueryAsset extends AssetBundle
class TestAssetLevel3 extends AssetBundle
{
public $basePath = '@testWebRoot/js';
public $baseUrl = '@testWeb/js';
public $basePath = '@webroot/js';
public $baseUrl = '@web/js';
}
class TestAssetCircleA extends AssetBundle
{
public $basePath = '@testWebRoot/js';
public $baseUrl = '@testWeb/js';
public $basePath = '@webroot/js';
public $baseUrl = '@web/js';
public $js = [
'jquery.js',
];
@ -402,8 +456,8 @@ class TestAssetCircleA extends AssetBundle
class TestAssetCircleB extends AssetBundle
{
public $basePath = '@testWebRoot/js';
public $baseUrl = '@testWeb/js';
public $basePath = '@webroot/js';
public $baseUrl = '@web/js';
public $js = [
'jquery.js',
];
@ -414,8 +468,8 @@ class TestAssetCircleB extends AssetBundle
class TestAssetPerFileOptions extends AssetBundle
{
public $basePath = '@testWebRoot';
public $baseUrl = '@testWeb';
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'default_options.css',
['tv.css', 'media' => 'tv'],

Loading…
Cancel
Save