Page MenuHomec4science

PhutilMarkupTestCase.php
No OneTemporary

File Metadata

Created
Wed, Dec 4, 00:38

PhutilMarkupTestCase.php

<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @group testcase
*/
class PhutilMarkupTestCase extends ArcanistPhutilTestCase {
public function testTagDefaults() {
$this->assertEqual(
phutil_render_tag('x'),
phutil_render_tag('x', array()));
$this->assertEqual(
phutil_render_tag('x', array()),
phutil_render_tag('x', array(), null));
}
public function testTagEmpty() {
$this->assertEqual(
'<x />',
phutil_render_tag('x', array(), null));
$this->assertEqual(
'<x></x>',
phutil_render_tag('x', array(), ''));
}
public function testTagBasics() {
$this->assertEqual(
'<x />',
phutil_render_tag('x'));
$this->assertEqual(
'<x>y</x>',
phutil_render_tag('x', array(), 'y'));
}
public function testTagAttributes() {
$this->assertEqual(
'<x u="v">y</x>',
phutil_render_tag('x', array('u' => 'v'), 'y'));
$this->assertEqual(
'<x u="v" />',
phutil_render_tag('x', array('u' => 'v')));
}
public function testTagEscapes() {
$this->assertEqual(
'<x u="&lt;" />',
phutil_render_tag('x', array('u' => '<')));
$this->assertEqual(
'<x><y /></x>',
phutil_render_tag('x', array(), phutil_render_tag('y')));
}
public function testTagJavascriptProtocolRejection() {
$hrefs = array(
'javascript:alert(1)' => true,
'JAVASCRIPT:alert(1)' => true,
' javascript:alert(1)' => true,
'/' => false,
'/path/to/stuff/' => false,
'' => false,
'http://example.com/' => false,
'#' => false,
);
foreach (array(true, false) as $use_uri) {
foreach ($hrefs as $href => $expect) {
if ($use_uri) {
$href = new PhutilURI($href);
}
$caught = null;
try {
phutil_render_tag('a', array('href' => $href), 'click for candy');
} catch (Exception $ex) {
$caught = $ex;
}
$this->assertEqual(
$expect,
$caught instanceof Exception,
"Rejected href: {$href}");
}
}
}
}

Event Timeline