mockWebApplication([ 'components' => [ 'request' => [ 'scriptFile' => __DIR__ . '/baseUrl/index.php', 'scriptUrl' => '/baseUrl/index.php', ], ], ]); $view = new View(); $view->registerJsVar('username', 'samdark'); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString('', $html); $view = new View(); $view->registerJsVar( 'objectTest', [ 'number' => 42, 'question' => 'Unknown', ] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); } public function testRegisterJsFileWithAlias(): void { $this->mockWebApplication([ 'components' => [ 'request' => [ 'scriptFile' => __DIR__ . '/baseUrl/index.php', 'scriptUrl' => '/baseUrl/index.php', ], ], ]); $view = new View(); $view->registerJsFile('@web/js/somefile.js', ['position' => View::POS_HEAD]); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString('', $html); $view = new View(); $view->registerJsFile('@web/js/somefile.js', ['position' => View::POS_BEGIN]); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertContainsWithoutLE('' . PHP_EOL . '', $html); $view = new View(); $view->registerJsFile('@web/js/somefile.js', ['position' => View::POS_END]); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString('', $html); // alias with depends $view = new View(); $view->registerJsFile('@web/js/somefile.js', ['position' => View::POS_END, 'depends' => 'yii\web\AssetBundle']); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString('', $html); } public function testRegisterCssFileWithAlias(): void { $this->mockWebApplication([ 'components' => [ 'request' => [ 'scriptFile' => __DIR__ . '/baseUrl/index.php', 'scriptUrl' => '/baseUrl/index.php', ], ], ]); $view = new View(); $view->registerCssFile('@web/css/somefile.css'); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString('', $html); // with depends $view = new View(); $view->registerCssFile( '@web/css/somefile.css', ['position' => View::POS_END, 'depends' => 'yii\web\AssetBundle'] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString('', $html); } public function testRegisterregisterCsrfMetaTags(): void { $this->mockWebApplication([ 'components' => [ 'request' => [ 'scriptFile' => __DIR__ . '/baseUrl/index.php', 'scriptUrl' => '/baseUrl/index.php', ], 'cache' => [ 'class' => FileCache::class, ], ], ]); $view = new View(); $view->registerCsrfMetaTags(); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString('', $html); $this->assertStringContainsString('', $html); $this->assertStringContainsString('~', $html, $matches)) { $this->fail("No CSRF-token meta tag found. HTML was:\n$html"); } return $matches[1]; } private function setUpAliases(): void { Yii::setAlias('@web', '/'); Yii::setAlias('@webroot', '@yiiunit/data/web'); Yii::setAlias('@testAssetsPath', '@webroot/assets'); Yii::setAlias('@testAssetsUrl', '@web/assets'); Yii::setAlias('@testSourcePath', '@webroot/assetSources'); } public function testAppendTimestampForRegisterJsFile(): void { $this->mockWebApplication([ 'components' => [ 'request' => [ 'scriptFile' => __DIR__ . '/baseUrl/index.php', 'scriptUrl' => '/baseUrl/index.php', ], ], ]); $this->setUpAliases(); $pattern = '/assetSources\/js\/jquery\.js\?v\=\d+"/'; Yii::$app->assetManager->appendTimestamp = true; // will be used AssetManager and timestamp $view = new View(); $view->registerJsFile( '/assetSources/js/jquery.js', ['depends' => 'yii\web\AssetBundle'] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // test append timestamp when @web is prefixed in url Yii::setAlias('@web', '/test-app'); $view = new View(); $view->registerJsFile( Yii::getAlias('@web/assetSources/js/jquery.js'), ['depends' => 'yii\web\AssetBundle'] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // test append timestamp when @web has the same name as the asset-source folder Yii::setAlias('@web', '/assetSources/'); $view = new View(); $view->registerJsFile( Yii::getAlias('@web/assetSources/js/jquery.js'), ['depends' => 'yii\web\AssetBundle'] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // reset aliases $this->setUpAliases(); // won't be used AssetManager but the timestamp will be $view = new View(); $view->registerJsFile('/assetSources/js/jquery.js'); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); $view = new View(); $view->registerJsFile( '/assetSources/js/jquery.js', ['appendTimestamp' => true] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // redefine AssetManager timestamp setting $view = new View(); $view->registerJsFile( '/assetSources/js/jquery.js', ['appendTimestamp' => false] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); // with alias $view = new View(); $view->registerJsFile('@web/assetSources/js/jquery.js'); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // with alias but wo timestamp // redefine AssetManager timestamp setting $view = new View(); $view->registerJsFile( '@web/assetSources/js/jquery.js', [ 'appendTimestamp' => false, 'depends' => 'yii\web\AssetBundle', ] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); // wo depends == wo AssetManager $view = new View(); $view->registerJsFile( '@web/assetSources/js/jquery.js', ['appendTimestamp' => false] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); // absolute link $view = new View(); $view->registerJsFile('http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); $view = new View(); $view->registerJsFile( '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', ['depends' => 'yii\web\AssetBundle'] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); $view = new View(); $view->registerJsFile( 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', ['depends' => 'yii\web\AssetBundle'] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); Yii::$app->assetManager->appendTimestamp = false; $view = new View(); $view->registerJsFile( '/assetSources/js/jquery.js', ['depends' => 'yii\web\AssetBundle'] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); $view = new View(); $view->registerJsFile('/assetSources/js/jquery.js'); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); $view = new View(); $view->registerJsFile( '/assetSources/js/jquery.js', ['appendTimestamp' => true] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // redefine AssetManager timestamp setting $view = new View(); $view->registerJsFile( '/assetSources/js/jquery.js', [ 'appendTimestamp' => true, 'depends' => 'yii\web\AssetBundle', ] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); $view = new View(); $view->registerJsFile( '/assetSources/js/jquery.js', ['appendTimestamp' => false] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); // absolute link $view = new View(); $view->registerJsFile('http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); $view = new View(); $view->registerJsFile( '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', ['depends' => 'yii\web\AssetBundle'] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); $view = new View(); $view->registerJsFile( 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', ['depends' => 'yii\web\AssetBundle'] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); } public function testAppendTimestampForRegisterCssFile(): void { $this->mockWebApplication([ 'components' => [ 'request' => [ 'scriptFile' => __DIR__ . '/baseUrl/index.php', 'scriptUrl' => '/baseUrl/index.php', ], ], ]); $this->setUpAliases(); $pattern = '/assetSources\/css\/stub\.css\?v\=\d+"/'; Yii::$app->assetManager->appendTimestamp = true; // will be used AssetManager and timestamp $view = new View(); $view->registerCssFile( '/assetSources/css/stub.css', ['depends' => 'yii\web\AssetBundle'] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // test append timestamp when @web is prefixed in url Yii::setAlias('@web', '/test-app'); $view = new View(); $view->registerCssFile( Yii::getAlias('@web/assetSources/css/stub.css'), ['depends' => 'yii\web\AssetBundle'] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // test append timestamp when @web has the same name as the asset-source folder Yii::setAlias('@web', '/assetSources/'); $view = new View(); $view->registerCssFile( Yii::getAlias('@web/assetSources/css/stub.css'), ['depends' => 'yii\web\AssetBundle'] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // reset aliases $this->setUpAliases(); // won't be used AssetManager but the timestamp will be $view = new View(); $view->registerCssFile('/assetSources/css/stub.css'); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); $view = new View(); $view->registerCssFile( '/assetSources/css/stub.css', ['appendTimestamp' => true] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // redefine AssetManager timestamp setting $view = new View(); $view->registerCssFile( '/assetSources/css/stub.css', ['appendTimestamp' => false] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); // with alias $view = new View(); $view->registerCssFile('@web/assetSources/css/stub.css'); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // with alias but wo timestamp // redefine AssetManager timestamp setting $view = new View(); $view->registerCssFile( '@web/assetSources/css/stub.css', [ 'appendTimestamp' => false, 'depends' => 'yii\web\AssetBundle', ] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); // wo depends == wo AssetManager $view = new View(); $view->registerCssFile( '@web/assetSources/css/stub.css', ['appendTimestamp' => false] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); // absolute link $view = new View(); $view->registerCssFile('https://cdnjs.cloudflare.com/ajax/libs/balloon-css/1.0.3/balloon.css'); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); $view = new View(); $view->registerCssFile( '//cdnjs.cloudflare.com/ajax/libs/balloon-css/1.0.3/balloon.css', ['depends' => 'yii\web\AssetBundle'] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); $view = new View(); $view->registerCssFile( 'https://cdnjs.cloudflare.com/ajax/libs/balloon-css/1.0.3/balloon.css', ['depends' => 'yii\web\AssetBundle'] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); Yii::$app->assetManager->appendTimestamp = false; $view = new View(); $view->registerCssFile( '/assetSources/css/stub.css', ['depends' => 'yii\web\AssetBundle'] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); $view = new View(); $view->registerCssFile('/assetSources/css/stub.css'); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); $view = new View(); $view->registerCssFile( '/assetSources/css/stub.css', ['appendTimestamp' => true] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); // redefine AssetManager timestamp setting $view = new View(); $view->registerCssFile( '/assetSources/css/stub.css', [ 'appendTimestamp' => true, 'depends' => 'yii\web\AssetBundle', ] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertMatchesRegularExpression($pattern, $html); $view = new View(); $view->registerCssFile( '/assetSources/css/stub.css', ['appendTimestamp' => false] ); // $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertDoesNotMatchRegularExpression($pattern, $html); // absolute link $view = new View(); $view->registerCssFile('https://cdnjs.cloudflare.com/ajax/libs/balloon-css/1.0.3/balloon.css'); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); $view = new View(); $view->registerCssFile( '//cdnjs.cloudflare.com/ajax/libs/balloon-css/1.0.3/balloon.css', ['depends' => 'yii\web\AssetBundle'] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); $view = new View(); $view->registerCssFile( 'https://cdnjs.cloudflare.com/ajax/libs/balloon-css/1.0.3/balloon.css', ['depends' => 'yii\web\AssetBundle'] ); $html = $view->render('@yiiunit/data/views/layout.php', ['content' => 'content']); $this->assertStringContainsString( '', $html ); } }