Merge pull request #1093 from jim-parry/testing2/helpers

Testing2/helpers
This commit is contained in:
Lonnie Ezell 2018-07-07 23:15:32 -05:00 committed by GitHub
commit db1948c840
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 2086 additions and 1752 deletions

View File

@ -8,7 +8,7 @@
class DocTypes
{
static $list =
public $list =
[
'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
'xhtml1-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',

View File

@ -48,12 +48,12 @@ if ( ! function_exists('form_open'))
* Creates the opening portion of the form.
*
* @param string $action the URI segments of the form destination
* @param array $attributes a key/value pair of attributes
* @param array|string $attributes a key/value pair of attributes, or string representation
* @param array $hidden a key/value pair hidden data
*
* @return string
*/
function form_open(string $action = '', array $attributes = [], array $hidden = []): string
function form_open(string $action = '', $attributes = [], array $hidden = []): string
{
// If no action is provided then set to the current url
if ( ! $action)
@ -82,7 +82,7 @@ if ( ! function_exists('form_open'))
$form = '<form action="' . $action . '"' . $attributes . ">\n";
// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
$before = (new \Config\Filters())->globals['before'];
$before = Services::filters()->getFilters()['before'];
if ((in_array('csrf', $before) || array_key_exists('csrf', $before)) && strpos($action, base_url()) !== false && ! stripos($form, 'method="get"')
)
@ -114,16 +114,16 @@ if ( ! function_exists('form_open_multipart'))
* Creates the opening portion of the form, but with "multipart/form-data".
*
* @param string $action The URI segments of the form destination
* @param array $attributes A key/value pair of attributes
* @param array|string $attributes A key/value pair of attributes, or the same as a string
* @param array $hidden A key/value pair hidden data
*
* @return string
*/
function form_open_multipart(string $action = '', array $attributes = [], array $hidden = []): string
function form_open_multipart(string $action = '', $attributes = [], array $hidden = []): string
{
if (is_string($attributes))
{
$attributes .= ' enctype="multipart/form-data"';
$attributes .= ' enctype="' . esc('multipart/form-data', 'attr') . '"';
}
else
{
@ -208,9 +208,9 @@ if ( ! function_exists('form_input'))
function form_input($data = '', string $value = '', $extra = '', string $type = 'text'): string
{
$defaults = [
'type' => $type,
'name' => is_array($data) ? '' : $data,
'value' => $value,
'type' => $type,
'name' => is_array($data) ? '' : $data,
'value' => $value,
];
return '<input ' . parse_form_attributes($data, $defaults) . stringify_attributes($extra) . " />\n";
@ -288,9 +288,9 @@ if ( ! function_exists('form_textarea'))
function form_textarea($data = '', string $value = '', $extra = ''): string
{
$defaults = [
'name' => is_array($data) ? '' : $data,
'cols' => '40',
'rows' => '10',
'name' => is_array($data) ? '' : $data,
'cols' => '40',
'rows' => '10',
];
if ( ! is_array($data) || ! isset($data['value']))
{
@ -515,9 +515,9 @@ if ( ! function_exists('form_submit'))
function form_submit($data = '', string $value = '', $extra = ''): string
{
$defaults = [
'type' => 'submit',
'name' => is_array($data) ? '' : $data,
'value' => $value,
'type' => 'submit',
'name' => is_array($data) ? '' : $data,
'value' => $value,
];
return '<input ' . parse_form_attributes($data, $defaults) . stringify_attributes($extra) . " />\n";
@ -542,9 +542,9 @@ if ( ! function_exists('form_reset'))
function form_reset($data = '', string $value = '', $extra = ''): string
{
$defaults = [
'type' => 'reset',
'name' => is_array($data) ? '' : $data,
'value' => $value,
'type' => 'reset',
'name' => is_array($data) ? '' : $data,
'value' => $value,
];
return '<input ' . parse_form_attributes($data, $defaults) . stringify_attributes($extra) . " />\n";
@ -569,8 +569,8 @@ if ( ! function_exists('form_button'))
function form_button($data = '', string $content = '', $extra = ''): string
{
$defaults = [
'name' => is_array($data) ? '' : $data,
'type' => 'button',
'name' => is_array($data) ? '' : $data,
'type' => 'button',
];
if (is_array($data) && isset($data['content']))
@ -643,10 +643,10 @@ if ( ! function_exists('form_datalist'))
function form_datalist($name, $value, $options)
{
$data = [
'type' => 'text',
'name' => $name,
'list' => $name . '_list',
'value' => $value,
'type' => 'text',
'name' => $name,
'list' => $name . '_list',
'value' => $value,
];
$out = form_input($data) . "\n";
@ -868,7 +868,7 @@ if ( ! function_exists('set_checkbox'))
}
// Unchecked checkbox and radio inputs are not even submitted by browsers ...
if (! empty($request->getPost()) || ! empty(old($field)))
if ( ! empty($request->getPost()) || ! empty(old($field)))
{
return ($input === $value) ? ' checked="checked"' : '';
}
@ -904,7 +904,6 @@ if ( ! function_exists('set_radio'))
// Try any old input data we may have first
$input = $request->getOldInput($field);
if ($input === null)
{
$input = $request->getPost($field) ?? $default;
@ -925,12 +924,15 @@ if ( ! function_exists('set_radio'))
}
// Unchecked checkbox and radio inputs are not even submitted by browsers ...
$result = '';
if ($request->getPost())
{
return ($input === $value) ? ' checked="checked"' : '';
$result = ($input === $value) ? ' checked="checked"' : '';
}
return ($default === true) ? ' checked="checked"' : '';
if (empty($result))
$result = ($default === true) ? ' checked="checked"' : '';
return $result;
}
}
@ -962,7 +964,7 @@ if ( ! function_exists('parse_form_attributes'))
unset($attributes[$key]);
}
}
if (! empty($attributes))
if ( ! empty($attributes))
{
$default = array_merge($default, $attributes);
}

View File

@ -97,22 +97,13 @@ if ( ! function_exists('_list'))
* Generates an HTML ordered list from an single or multi-dimensional array.
*
* @param string $type
* @param array $list
* @param mixed $list
* @param string $attributes
* @param int $depth
* @return string
*/
function _list
(
string $type = 'ul', array $list = [], string $attributes = '', int $depth = 0
): string
function _list(string $type = 'ul', $list = [], string $attributes = '', int $depth = 0): string
{
// If an array wasn't submitted there's nothing to do...
if ( ! is_array($list))
{
return $list;
}
// Set the indentation based on the depth
$out = str_repeat(' ', $depth)
// Write the opening list tag
@ -162,20 +153,16 @@ if ( ! function_exists('img'))
*
* @param mixed $src
* @param bool $indexPage
* @param string $attributes
* @param mixed $attributes
* @return string
*/
function img
(
$src = '', bool $indexPage = false, string $attributes = ''
): string
function img($src = '', bool $indexPage = false, $attributes = ''): string
{
if ( ! is_array($src))
{
$src = ['src' => $src];
}
//If there is no alt attribute defined, set it to an empty string.
if ( ! isset($src['alt']))
{
@ -223,20 +210,13 @@ if ( ! function_exists('doctype'))
* xhtml-frame, html4-strict, html4-trans, and html4-frame.
* All values are saved in the doctypes config file.
*
* @param mixed $type The doctype to be generated
* @param string $type The doctype to be generated
* @return string
*/
function doctype($type = 'html5'): string
function doctype(string $type = 'html5'): string
{
$doctypes = null;
$env = ENVIRONMENT;
$doctypes = Config\DocTypes::$list;
$customDocTypesPath = APPPATH . "Config/{$env}/DocTypes.php";
if (file_exists($customDocTypesPath))
{
$customDocTypesNs = "Config\{$env}\DocTypes";
$doctypes = $customDocTypesNs::$list;
}
$config = new \Config\DocTypes();
$doctypes = $config->list;
return $doctypes[$type] ?? false;
}
@ -256,47 +236,30 @@ if ( ! function_exists('script_tag'))
* @param bool $indexPage Should indexPage be added to the JS path
* @return string
*/
function script_tag
(
$src = '', bool $indexPage = false
): string
function script_tag($src = '', bool $indexPage = false): string
{
$script = '<script ';
if (is_array($src))
if ( ! is_array($src))
{
foreach ($src as $k => $v)
$src = ['src' => $src];
}
foreach ($src as $k => $v)
{
if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v))
{
if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v))
if ($indexPage === true)
{
if ($indexPage === true)
{
$script .= 'src="' . site_url($v) . '" ';
}
else
{
$script .= 'src="' . slash_item('baseURL') . $v . '" ';
}
$script .= 'src="' . site_url($v) . '" ';
}
else
{
$script .= $k . '="' . $v . '" ';
$script .= 'src="' . slash_item('baseURL') . $v . '" ';
}
}
}
else
{
if (preg_match('#^([a-z]+:)?//#i', $src))
{
$script .= 'src="' . $src . '" ';
}
elseif ($indexPage === true)
{
$script .= 'src="' . site_url($src) . '" ';
}
else
{
$script .= 'src="' . slash_item('baseURL') . $src . '" ';
$script .= $k . '="' . $v . '" ';
}
}
@ -315,7 +278,7 @@ if ( ! function_exists('link_tag'))
*
* Generates link to a CSS file
*
* @param mixed $href Stylesheet hrefs or an array
* @param mixed $href Stylesheet href or an array
* @param string $rel
* @param string $type
* @param string $title
@ -323,41 +286,24 @@ if ( ! function_exists('link_tag'))
* @param bool $indexPage should indexPage be added to the CSS path.
* @return string
*/
function link_tag
(
$href = '', string $rel = 'stylesheet', string $type = 'text/css', string $title = '', string $media = '', bool $indexPage = false
): string
function link_tag($href = '', string $rel = 'stylesheet', string $type = 'text/css', string $title = '', string $media = '', bool $indexPage = false): string
{
$link = '<link ';
// extract fields if needed
if (is_array($href))
{
foreach ($href as $k => $v)
{
if ($k === 'href' && ! preg_match('#^([a-z]+:)?//#i', $v))
{
if ($indexPage === true)
{
$link .= 'href="' . site_url($v) . '" ';
}
else
{
$link .= 'href="' . slash_item('baseURL') . $v . '" ';
}
}
else
{
$link .= $k . '="' . $v . '" ';
}
}
$rel = $href['rel'] ?? $rel;
$type = $href['type'] ?? $type;
$title = $href['title'] ?? $title;
$media = $href['media'] ?? $media;
$indexPage = $href['indexPage'] ?? $indexPage;
$href = $href['href'] ?? '';
}
else
if ( ! preg_match('#^([a-z]+:)?//#i', $href))
{
if (preg_match('#^([a-z]+:)?//#i', $href))
{
$link .= 'href="' . $href . '" ';
}
elseif ($indexPage === true)
if ($indexPage === true)
{
$link .= 'href="' . site_url($href) . '" ';
}
@ -365,18 +311,20 @@ if ( ! function_exists('link_tag'))
{
$link .= 'href="' . slash_item('baseURL') . $href . '" ';
}
}
else
$link .= 'href="' . $href . '" ';
$link .= 'rel="' . $rel . '" type="' . $type . '" ';
$link .= 'rel="' . $rel . '" type="' . $type . '" ';
if ($media !== '')
{
$link .= 'media="' . $media . '" ';
}
if ($media !== '')
{
$link .= 'media="' . $media . '" ';
}
if ($title !== '')
{
$link .= 'title="' . $title . '" ';
}
if ($title !== '')
{
$link .= 'title="' . $title . '" ';
}
return $link . "/>";
@ -393,27 +341,20 @@ if ( ! function_exists('link_tag'))
* Geneartes a video element to embed videos. The video element can
* contain one or more video sources
*
* @param mixed $src Either a source string or
* an array of sources
* @param mixed $src Either a source string or an array of sources
* @param string $unsupportedMessage The message to display
* if the media tag is not supported by the browser
* if the media tag is not supported by the browser
* @param string $attributes HTML attributes
* @param array $tracks
* @param bool $indexPage
* @return string
*
*/
function video
(
$src, string $unsupportedMessage = '', string $attributes = '', array $tracks = [], bool $indexPage = false
): string
function video($src, string $unsupportedMessage = '', string $attributes = '', array $tracks = [], bool $indexPage = false): string
{
if (is_array($src))
{
return _media
(
'video', $src, $unsupportedMessage, $attributes, $tracks
);
return _media('video', $src, $unsupportedMessage, $attributes, $tracks);
}
$video = '<video';
@ -478,17 +419,11 @@ if ( ! function_exists('link_tag'))
*
* @return string
*/
function audio
(
$src, string $unsupportedMessage = '', string $attributes = '', array $tracks = [], bool $indexPage = false
): string
function audio($src, string $unsupportedMessage = '', string $attributes = '', array $tracks = [], bool $indexPage = false): string
{
if (is_array($src))
{
return _media
(
'audio', $src, $unsupportedMessage, $attributes, $tracks
);
return _media('audio', $src, $unsupportedMessage, $attributes, $tracks);
}
$audio = '<audio';
@ -517,15 +452,13 @@ if ( ! function_exists('link_tag'))
{
foreach ($tracks as $track)
{
$audio .= $track;
$audio .= "\n" . _space_indent() . $track;
}
}
if ( ! empty($unsupportedMessage))
{
$video .= _space_indent()
. $unsupportedMessage
. "\n";
$audio .= "\n" . _space_indent() . $unsupportedMessage . "\n";
}
$audio .= "</audio>\n";
@ -540,10 +473,7 @@ if ( ! function_exists('link_tag'))
if ( ! function_exists('_media'))
{
function _media
(
string $name, array $types, string $unsupportedMessage = '', string $attributes = '', array $tracks = []
): string
function _media(string $name, array $types = [], string $unsupportedMessage = '', string $attributes = '', array $tracks = []): string
{
$media = '<' . $name;
@ -563,11 +493,7 @@ if ( ! function_exists('link_tag'))
$media .= _space_indent() . $option . "\n";
}
if (empty($tracks))
{
$media .= "/>";
}
else
if ( ! empty($tracks))
{
foreach ($tracks as $track)
{
@ -605,23 +531,17 @@ if ( ! function_exists('link_tag'))
* @param bool $indexPage
* @return string
*/
function source
(
string $src, string $type, string $attributes = '', bool $indexPage = false
): string
function source(string $src, string $type = 'unknown', string $attributes = '', bool $indexPage = false): string
{
if (_has_protocol($src))
{
//Do nothing.
}
elseif ($indexPage === true)
{
$src = site_url($src);
}
else
{
$src = slash_item('baseURL') . $src;
}
if ( ! _has_protocol($src))
if ($indexPage === true)
{
$src = site_url($src);
}
else
{
$src = slash_item('baseURL') . $src;
}
$source = '<source src="' . $src
. '" type="' . $type . '"';
@ -655,10 +575,7 @@ if ( ! function_exists('link_tag'))
* @param string $label
* @return string
*/
function track
(
string $src, string $kind, string $srcLanguage, string $label
): string
function track(string $src, string $kind, string $srcLanguage, string $label): string
{
return '<track src="' . $src
. '" kind="' . $kind
@ -689,23 +606,17 @@ if ( ! function_exists('link_tag'))
*
* @return string
*/
function object
(
string $data, string $type, string $attributes = '', array $params = [], bool $indexPage = false
): string
function object(string $data, string $type = 'unknown', string $attributes = '', array $params = [], bool $indexPage = false): string
{
if (_has_protocol($data))
{
//Do nothing.
}
elseif ($indexPage === true)
{
$data = site_url($data);
}
else
{
$data = slash_item('baseURL') . $data;
}
if ( ! _has_protocol($data))
if ($indexPage === true)
{
$data = site_url($data);
}
else
{
$data = slash_item('baseURL') . $data;
}
$object = '<object data="' . $data . '" '
. $attributes . '>';
@ -744,10 +655,7 @@ if ( ! function_exists('link_tag'))
* @param string $attributes HTML attributes
* @return string
*/
function param
(
string $name, string $value, string $type = 'ref', string $attributes = ''
): string
function param(string $name, string $value, string $type = 'ref', string $attributes = ''): string
{
return '<param name="' . $name
. '" type="' . $type
@ -773,23 +681,17 @@ if ( ! function_exists('link_tag'))
* @param bool $indexPage
* @return string
*/
function embed
(
string $src, string $type, string $attributes = '', bool $indexPage = false
): string
function embed(string $src, string $type='unknown', string $attributes = '', bool $indexPage = false): string
{
if (_has_protocol($src))
{
//Do nothing.
}
elseif ($indexPage === true)
{
$src = site_url($src);
}
else
{
$src = slash_item('baseURL') . $src;
}
if ( ! _has_protocol($src))
if ($indexPage === true)
{
$src = site_url($src);
}
else
{
$src = slash_item('baseURL') . $src;
}
return '<embed src="' . $src
. '" type="' . $type . '" '

View File

@ -49,20 +49,21 @@ if ( ! function_exists('number_to_size'))
*/
function number_to_size($num, int $precision = 1, string $locale = null)
{
// Strip any formatting
$num = 0 + str_replace(',', '', $num);
// Can't work with non-numbers...
if ( ! is_numeric($num))
// Strip any formatting & ensure numeric input
try
{
$num = 0 + str_replace(',', '', $num);
}
catch (\ErrorException $ee)
{
return false;
}
// ignore sub part
$genralLocale = $locale;
if( ! empty($locale) && ( $underscorePos = strpos( $locale, '_' )))
if ( ! empty($locale) && ( $underscorePos = strpos($locale, '_')))
{
$genralLocale = substr( $locale, 0, $underscorePos );
$genralLocale = substr($locale, 0, $underscorePos);
}
if ($num >= 1000000000000)
@ -119,22 +120,24 @@ if ( ! function_exists('number_to_amount'))
*/
function number_to_amount($num, int $precision = 0, string $locale = null)
{
// Strip any formatting
$num = 0 + str_replace(',', '', $num);
// Can't work with non-numbers...
if ( ! is_numeric($num))
// Strip any formatting & ensure numeric input
try
{
$num = 0 + str_replace(',', '', $num);
}
catch (\ErrorException $ee)
{
return false;
}
$suffix = '';
// ignore sub part
$genralLocale = $locale;
if( ! empty($locale) && ( $underscorePos = strpos( $locale, '_' )))
if ( ! empty($locale) && ( $underscorePos = strpos($locale, '_')))
{
$genralLocale = substr( $locale, 0, $underscorePos );
$genralLocale = substr($locale, 0, $underscorePos);
}
if ($num > 1000000000000000)
@ -172,6 +175,7 @@ if ( ! function_exists('number_to_amount'))
if ( ! function_exists('number_to_currency'))
{
/**
* @param float $num
* @param string $currency
@ -182,8 +186,8 @@ if ( ! function_exists('number_to_currency'))
function number_to_currency($num, string $currency, string $locale = null)
{
return format_number($num, 1, $locale, [
'type' => NumberFormatter::CURRENCY,
'currency' => $currency
'type' => NumberFormatter::CURRENCY,
'currency' => $currency
]);
}
@ -258,49 +262,48 @@ if ( ! function_exists('format_number'))
if ( ! function_exists('number_to_roman'))
{
/**
* Convert a number to a roman numeral.
*
* @param int $num it will convert to int
*
* @return string
*/
* Convert a number to a roman numeral.
*
* @param int $num it will convert to int
*
* @return string
*/
function number_to_roman($num)
{
$num = (int) $num;
if ($num < 1 || $num > 3999)
{
return;
return;
}
$_number_to_roman = function($num, $th) use ( &$_number_to_roman ) {
$return = '';
$key1 = NULL;
$key2 = NULL;
switch ($th)
{
switch ($th) {
case 1:
$key1 = 'I';
$key2 = 'V';
$key1 = 'I';
$key2 = 'V';
$key_f = 'X';
break;
case 2:
$key1 = 'X';
$key2 = 'L';
$key1 = 'X';
$key2 = 'L';
$key_f = 'C';
break;
case 3:
$key1 = 'C';
$key2 = 'D';
$key1 = 'C';
$key2 = 'D';
$key_f = 'M';
break;
case 4:
$key1 = 'M';
$key1 = 'M';
break;
}
$n = $num % 10;
switch ($n)
{
switch ($n) {
case 1:
case 2:
case 3:
@ -321,8 +324,7 @@ if ( ! function_exists('number_to_roman'))
$return = $key1 . $key_f;
break;
}
switch ($num)
{
switch ($num) {
case 10:
$return = $key_f;
break;
@ -335,4 +337,5 @@ if ( ! function_exists('number_to_roman'))
};
return $_number_to_roman($num, 1);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -102,21 +102,11 @@ if ( ! function_exists('base_url'))
$path = implode('/', $path);
}
// We should be using the set baseURL the user set
// otherwise get rid of the path because we have
// We should be using the configured baseURL that the user set;
// otherwise get rid of the path, because we have
// no way of knowing the intent...
$config = \CodeIgniter\Config\Services::request()->config;
if ( ! empty($config->baseURL))
{
$url = new \CodeIgniter\HTTP\URI($config->baseURL);
}
else
{
$url = \CodeIgniter\Config\Services::request($config, false)->uri;
$url->setPath('/');
}
$url = new \CodeIgniter\HTTP\URI($config->baseURL);
unset($config);
// Merge in the path set by the user, if any
@ -402,7 +392,7 @@ if ( ! function_exists('safe_mailto'))
$x = str_split('<a href="mailto:', 1);
for ($i = 0, $l = strlen($email); $i < $l; $i ++)
for ($i = 0, $l = strlen($email); $i < $l; $i ++ )
{
$x[] = '|' . ord($email[$i]);
}
@ -416,7 +406,7 @@ if ( ! function_exists('safe_mailto'))
foreach ($attributes as $key => $val)
{
$x[] = ' ' . $key . '="';
for ($i = 0, $l = strlen($val); $i < $l; $i ++)
for ($i = 0, $l = strlen($val); $i < $l; $i ++ )
{
$x[] = '|' . ord($val[$i]);
}
@ -425,7 +415,7 @@ if ( ! function_exists('safe_mailto'))
}
else
{
for ($i = 0, $l = strlen($attributes); $i < $l; $i ++)
for ($i = 0, $l = strlen($attributes); $i < $l; $i ++ )
{
$x[] = $attributes[$i];
}
@ -435,7 +425,7 @@ if ( ! function_exists('safe_mailto'))
$x[] = '>';
$temp = [];
for ($i = 0, $l = strlen($title); $i < $l; $i ++)
for ($i = 0, $l = strlen($title); $i < $l; $i ++ )
{
$ordinal = ord($title[$i]);
@ -473,7 +463,7 @@ if ( ! function_exists('safe_mailto'))
. "//<![CDATA["
. "var l=new Array();";
for ($i = 0, $c = count($x); $i < $c; $i ++)
for ($i = 0, $c = count($x); $i < $c; $i ++ )
{
$output .= "l[" . $i . "] = '" . $x[$i] . "';";
}
@ -605,10 +595,10 @@ if ( ! function_exists('url_title'))
$q_separator = preg_quote($separator, '#');
$trans = [
'&.+?;' => '',
'[^\w\d _-]' => '',
'\s+' => $separator,
'(' . $q_separator . ')+' => $separator
'&.+?;' => '',
'[^\w\d _-]' => '',
'\s+' => $separator,
'(' . $q_separator . ')+' => $separator
];
$str = strip_tags($str);

File diff suppressed because it is too large Load Diff

View File

@ -13,9 +13,8 @@ final class HTMLHelperTest extends \CIUnitTestCase
helper('url');
helper('html');
$this->tracks =
[
track('subtitles_no.vtt', 'subtitles', 'no', 'Norwegian No'),
$this->tracks = [
track('subtitles_no.vtt', 'subtitles', 'no', 'Norwegian No'),
track('subtitles_yes.vtt', 'subtitles', 'yes', 'Norwegian Yes')
];
}
@ -33,7 +32,7 @@ final class HTMLHelperTest extends \CIUnitTestCase
EOH;
$expected = ltrim($expected);
$list = ['foo', 'bar'];
$list = ['foo', 'bar'];
$this->assertEquals(ltrim($expected), ul($list));
}
@ -49,7 +48,7 @@ EOH;
EOH;
$expected = ltrim($expected);
$list = ['foo', 'bar'];
$list = ['foo', 'bar'];
$this->assertEquals($expected, ul($list, 'class="test"'));
}
@ -71,7 +70,7 @@ EOH;
EOH;
$expected = ltrim($expected);
$list = ['foo', 'bar', ['foo', 'bar']];
$list = ['foo', 'bar', ['foo', 'bar']];
$this->assertEquals(ltrim($expected), ul($list));
}
@ -89,7 +88,7 @@ EOH;
EOH;
$expected = ltrim($expected);
$list = ['foo', 'bar'];
$list = ['foo', 'bar'];
$this->assertEquals(ltrim($expected), ol($list));
}
@ -105,7 +104,7 @@ EOH;
EOH;
$expected = ltrim($expected);
$list = ['foo', 'bar'];
$list = ['foo', 'bar'];
$this->assertEquals($expected, ol($list, 'class="test"'));
}
@ -127,7 +126,7 @@ EOH;
EOH;
$expected = ltrim($expected);
$list = ['foo', 'bar', ['foo', 'bar']];
$list = ['foo', 'bar', ['foo', 'bar']];
$this->assertEquals(ltrim($expected), ol($list));
}
@ -136,45 +135,93 @@ EOH;
public function testIMG()
{
//TODO: Mock baseURL and siteURL.
$this->assertEquals
(
'<img src="http://site.com/images/picture.jpg" alt="" />',
img('http://site.com/images/picture.jpg')
);
$target = 'http://site.com/images/picture.jpg';
$expected = '<img src="http://site.com/images/picture.jpg" alt="" />';
$this->assertEquals($expected, img($target));
}
public function testIMGWithoutProtocol()
{
$target = 'assets/mugshot.jpg';
$expected = '<img src="http://example.com/assets/mugshot.jpg" alt="" />';
$this->assertEquals($expected, img($target));
}
public function testIMGWithIndexpage()
{
$target = 'assets/mugshot.jpg';
$expected = '<img src="http://example.com/index.php/assets/mugshot.jpg" alt="" />';
$this->assertEquals($expected, img($target, true));
}
// ------------------------------------------------------------------------
public function testScriptTag()
{
$this->assertEquals
(
'<script src="http://site.com/js/mystyles.js" type="text/javascript"></script>',
script_tag('http://site.com/js/mystyles.js')
);
$target = 'http://site.com/js/mystyles.js';
$expected = '<script src="http://site.com/js/mystyles.js" type="text/javascript"></script>';
$this->assertEquals($expected, script_tag($target));
}
public function testScriptTagWithoutProtocol()
{
$target = 'js/mystyles.js';
$expected = '<script src="http://example.com/js/mystyles.js" type="text/javascript"></script>';
$this->assertEquals($expected, script_tag($target));
}
public function testScriptTagWithIndexpage()
{
$target = 'js/mystyles.js';
$expected = '<script src="http://example.com/index.php/js/mystyles.js" type="text/javascript"></script>';
$this->assertEquals($expected, script_tag($target, true));
}
// ------------------------------------------------------------------------
public function testLinkTag()
{
$this->assertEquals
(
'<link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />',
link_tag('http://site.com/css/mystyles.css')
);
$target = 'css/mystyles.css';
$expected = '<link href="http://example.com/css/mystyles.css" rel="stylesheet" type="text/css" />';
$this->assertEquals($expected, link_tag($target));
}
public function testLinkTagComplete()
{
$target = 'https://styles.com/css/mystyles.css';
$expected = '<link href="https://styles.com/css/mystyles.css" rel="banana" type="fruit" media="VHS" title="Go away" />';
$this->assertEquals($expected, link_tag($target, 'banana', 'fruit', 'Go away', 'VHS'));
}
public function testLinkTagArray()
{
$parms = [
'href' => 'css/mystyles.css',
'indexPage' => true,
];
$expected = '<link href="http://example.com/index.php/css/mystyles.css" rel="stylesheet" type="text/css" />';
$this->assertEquals($expected, link_tag($parms));
}
// ------------------------------------------------------------------------
public function testDocType()
{
$this->assertEquals
(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
doctype('html4-strict')
);
$target = 'html4-strict';
$expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
$this->assertEquals($expected, doctype($target));
}
public function testDocTypeDefault()
{
$expected = '<!DOCTYPE html>';
$this->assertEquals($expected, doctype());
}
public function testDocTypeInvalid()
{
$target = 'good-guess';
$this->assertEquals(false, doctype($target));
}
// ------------------------------------------------------------------------
@ -182,23 +229,22 @@ EOH;
public function testVideo()
{
$expected = <<<EOH
<video src="http://example.com/test.mp4" controls>
<video src="http://www.codeigniter.com/test.mp4" controls>
Your browser does not support the video tag.
</video>
EOH;
$video = video
(
'test.mp4',
'Your browser does not support the video tag.',
'controls'
);
$target = 'http://www.codeigniter.com/test.mp4';
$message = 'Your browser does not support the video tag.';
$video = video($target, $message, 'controls');
$this->assertEquals($expected, $video);
}
public function testVideoWithTracks()
{
$expected = <<<EOH
<video src="http://www.codeigniter.com/test.mp4" controls>
<video src="http://example.com/test.mp4" controls>
<track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />
<track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" />
Your browser does not support the video tag.
@ -206,15 +252,31 @@ EOH;
EOH;
$video = video
(
'http://www.codeigniter.com/test.mp4',
'Your browser does not support the video tag.',
'controls',
$this->tracks
);
$target = 'test.mp4';
$message = 'Your browser does not support the video tag.';
$video = video($target, $message, 'controls', $this->tracks);
$this->assertEquals($expected, $video);
}
public function testVideoWithTracksAndIndex()
{
$expected = <<<EOH
<video src="http://example.com/index.php/test.mp4" controls>
<track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />
<track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" />
Your browser does not support the video tag.
</video>
EOH;
$target = 'test.mp4';
$message = 'Your browser does not support the video tag.';
$video = video($target, $message, 'controls', $this->tracks, true);
$this->assertEquals($expected, $video);
}
public function testVideoMultipleSources()
{
$expected = <<<EOH
<video class="test" controls>
<source src="http://example.com/movie.mp4" type="video/mp4" class="test" />
@ -228,20 +290,16 @@ EOH;
EOH;
$video = video
(
[
source('movie.mp4', 'video/mp4', 'class="test"'),
source('movie.ogg', 'video/ogg'),
source('movie.mov', 'video/quicktime'),
source('movie.ogv', 'video/ogv; codecs=dirac, speex')
],
'Your browser does not support the video tag.',
'class="test" controls',
$this->tracks
);
$sources = [
source('movie.mp4', 'video/mp4', 'class="test"'),
source('movie.ogg', 'video/ogg'),
source('movie.mov', 'video/quicktime'),
source('movie.ogv', 'video/ogv; codecs=dirac, speex')
];
$message = 'Your browser does not support the video tag.';
$video = video($sources, $message, 'class="test" controls', $this->tracks);
$this->assertEquals($expected, $video);
$this->assertEquals($expected, $video);
}
// ------------------------------------------------------------------------
@ -259,20 +317,116 @@ EOH;
EOH;
$audio = audio
(
[
source('sound.ogg', 'audio/ogg'),
source('sound.mpeg', 'audio/mpeg')
],
'Your browser does not support the audio tag.',
'id="test" controls',
$this->tracks
);
$sources = [
source('sound.ogg', 'audio/ogg'),
source('sound.mpeg', 'audio/mpeg')
];
$message = 'Your browser does not support the audio tag.';
$audio = audio($sources, $message, 'id="test" controls', $this->tracks);
$this->assertEquals($expected, $audio);
}
public function testAudioSimple()
{
$expected = <<<EOH
<audio src="http://example.com/sound.mpeg" type="audio/mpeg" id="test" controls>
Your browser does not support the audio tag.
</audio>
EOH;
$source = 'sound.mpeg';
$message = 'Your browser does not support the audio tag.';
$audio = audio($source, $message, 'type="audio/mpeg" id="test" controls');
$this->assertEquals($expected, $audio);
}
public function testAudioWithSource()
{
$expected = <<<EOH
<audio src="http://codeigniter.com/sound.mpeg" type="audio/mpeg" id="test" controls>
Your browser does not support the audio tag.
</audio>
EOH;
$source = 'http://codeigniter.com/sound.mpeg';
$message = 'Your browser does not support the audio tag.';
$audio = audio($source, $message, 'type="audio/mpeg" id="test" controls');
$this->assertEquals($expected, $audio);
}
public function testAudioWithIndex()
{
$expected = <<<EOH
<audio src="http://example.com/index.php/sound.mpeg" type="audio/mpeg" id="test" controls>
Your browser does not support the audio tag.
</audio>
EOH;
$source = 'sound.mpeg';
$message = 'Your browser does not support the audio tag.';
$audio = audio($source, $message, 'type="audio/mpeg" id="test" controls', [], true);
$this->assertEquals($expected, $audio);
}
public function testAudioWithTracks()
{
$expected = <<<EOH
<audio src="http://example.com/sound.mpeg" type="audio/mpeg" id="test" controls>
<track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />
<track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" />
Your browser does not support the audio tag.
</audio>
EOH;
$source = 'sound.mpeg';
$message = 'Your browser does not support the audio tag.';
$audio = audio($source, $message, 'type="audio/mpeg" id="test" controls', $this->tracks);
$this->assertEquals($expected, $audio);
}
// ------------------------------------------------------------------------
public function testMediaNameOnly()
{
$expected = <<<EOH
<av>
</av>
EOH;
$this->assertEquals($expected, _media('av'));
}
public function testMediaWithSources()
{
$expected = <<<EOH
<av>
<source src="http://example.com/sound.ogg" type="audio/ogg" />
<source src="http://example.com/sound.mpeg" type="audio/mpeg" />
</av>
EOH;
$sources = [
source('sound.ogg', 'audio/ogg'),
source('sound.mpeg', 'audio/mpeg')
];
$this->assertEquals($expected, _media('av', $sources));
}
public function testSource()
{
$expected = '<source src="http://example.com/index.php/sound.mpeg" type="audio/mpeg" />';
$this->assertEquals($expected, source('sound.mpeg', 'audio/mpeg', '', true));
}
// ------------------------------------------------------------------------
public function testEmbed()
@ -282,10 +436,23 @@ EOH;
EOH;
$embed = embed('movie.mov', 'video/quicktime', 'class="test"');
$type = 'video/quicktime';
$embed = embed('movie.mov', $type, 'class="test"');
$this->assertEquals($expected, $embed);
}
public function testEmbedIndexed()
{
$expected = <<<EOH
<embed src="http://example.com/index.php/movie.mov" type="video/quicktime" class="test" />
EOH;
$type = 'video/quicktime';
$embed = embed('movie.mov', $type, 'class="test"', true);
$this->assertEquals($expected, $embed, '');
}
public function testObject()
{
$expected = <<<EOH
@ -293,14 +460,14 @@ EOH;
EOH;
$object = object
(
'movie.swf',
'application/x-shockwave-flash',
'class="test"'
);
$type = 'application/x-shockwave-flash';
$object = object('movie.swf', $type, 'class="test"');
$this->assertEquals($expected, $object);
}
public function testObjectWithParams()
{
$expected = <<<EOH
<object data="http://example.com/movie.swf" class="test">
@ -310,19 +477,27 @@ EOH;
EOH;
$object = object
(
'movie.swf',
'application/x-shockwave-flash',
'class="test"',
[
param('foo', 'bar', 'ref', 'class="test"'),
param('hello', 'world', 'ref', 'class="test"')
]
);
$type = 'application/x-shockwave-flash';
$parms = [
param('foo', 'bar', 'ref', 'class="test"'),
param('hello', 'world', 'ref', 'class="test"')
];
$object = object('movie.swf', $type, 'class="test"', $parms);
$this->assertEquals($expected, $object);
}
public function testObjectIndexed()
{
$expected = <<<EOH
<object data="http://example.com/index.php/movie.swf" class="test"></object>
EOH;
$type = 'application/x-shockwave-flash';
$object = object('movie.swf', $type, 'class="test"', [], true);
$this->assertEquals($expected, $object);
}
// ------------------------------------------------------------------------
}

View File

@ -2,98 +2,123 @@
class numberHelperTest extends \CIUnitTestCase
{
public function setUp()
{
parent::setUp();
helper('number');
}
}
public function test_roman_number()
{
$this->assertEquals('XCVI', number_to_roman(96));
$this->assertEquals('MMDCCCXCV', number_to_roman(2895));
$this->assertEquals('CCCXXIX', number_to_roman(329));
}
public function test_roman_number()
{
$this->assertEquals('XCVI', number_to_roman(96));
$this->assertEquals('MMDCCCXCV', number_to_roman(2895));
$this->assertEquals('CCCXXIX', number_to_roman(329));
$this->assertEquals('IV', number_to_roman(4));
$this->assertEquals('X', number_to_roman(10));
}
public function test_format_number()
{
$this->assertEquals('123,456', format_number(123456, 0, 'en_US'));
}
public function testRomanNumberRange()
{
$this->assertEquals(null, number_to_roman(-1));
$this->assertEquals(null, number_to_roman(0));
$this->assertEquals(null, number_to_roman(4000));
}
public function test_format_number_with_precision()
{
$this->assertEquals('123,456.8', format_number(123456.789, 1, 'en_US'));
$this->assertEquals('123,456.79', format_number(123456.789, 2, 'en_US'));
}
public function test_format_number()
{
$this->assertEquals('123,456', format_number(123456, 0, 'en_US'));
}
public function test_number_to_size()
{
$this->assertEquals('456 Bytes', number_to_size(456, 1, 'en_US'));
}
public function test_format_number_with_precision()
{
$this->assertEquals('123,456.8', format_number(123456.789, 1, 'en_US'));
$this->assertEquals('123,456.79', format_number(123456.789, 2, 'en_US'));
}
public function test_kb_format()
{
$this->assertEquals('4.5 KB', number_to_size(4567, 1, 'en_US'));
}
public function testFormattingOptions()
{
$options = [
'before' => '<<',
'after' => '>>',
];
$this->assertEquals('<<123,456.79>>', format_number(123456.789, 2, 'en_US', $options));
}
public function test_kb_format_medium()
{
$this->assertEquals('44.6 KB', number_to_size(45678, 1, 'en_US'));
}
public function test_number_to_size()
{
$this->assertEquals('456 Bytes', number_to_size(456, 1, 'en_US'));
}
public function test_kb_format_large()
{
$this->assertEquals('446.1 KB', number_to_size(456789, 1, 'en_US'));
}
public function test_kb_format()
{
$this->assertEquals('4.5 KB', number_to_size(4567, 1, 'en_US'));
}
public function test_mb_format()
{
$this->assertEquals('3.3 MB', number_to_size(3456789, 1, 'en_US'));
}
public function test_kb_format_medium()
{
$this->assertEquals('44.6 KB', number_to_size(45678, 1, 'en_US'));
}
public function test_gb_format()
{
$this->assertEquals('1.8 GB', number_to_size(1932735283.2, 1, 'en_US'));
}
public function test_kb_format_large()
{
$this->assertEquals('446.1 KB', number_to_size(456789, 1, 'en_US'));
}
public function test_tb_format()
{
$this->assertEquals('112,283.3 TB', number_to_size(123456789123456789, 1, 'en_US'));
}
public function test_mb_format()
{
$this->assertEquals('3.3 MB', number_to_size(3456789, 1, 'en_US'));
}
public function test_thousands()
{
$this->assertEquals('123 thousand', number_to_amount('123,000', 0, 'en_US'));
}
public function test_gb_format()
{
$this->assertEquals('1.8 GB', number_to_size(1932735283.2, 1, 'en_US'));
}
public function test_millions()
{
$this->assertEquals('123.4 million', number_to_amount('123,400,000', 1, 'en_US'));
}
public function test_tb_format()
{
$this->assertEquals('112,283.3 TB', number_to_size(123456789123456789, 1, 'en_US'));
}
public function test_billions()
{
$this->assertEquals('123.46 billion', number_to_amount('123,456,000,000', 2, 'en_US'));
}
public function test_thousands()
{
$this->assertEquals('123 thousand', number_to_amount('123,000', 0, 'en_US'));
}
public function test_trillions()
{
$this->assertEquals('123.457 trillion', number_to_amount('123,456,700,000,000', 3, 'en_US'));
}
public function test_millions()
{
$this->assertEquals('123.4 million', number_to_amount('123,400,000', 1, 'en_US'));
}
public function test_quadrillions()
{
$this->assertEquals('123.5 quadrillion', number_to_amount('123,456,700,000,000,000', 1, 'en_US'));
}
public function test_billions()
{
$this->assertEquals('123.46 billion', number_to_amount('123,456,000,000', 2, 'en_US'));
}
/**
* @group single
*/
public function test_currency_current_locale()
{
$this->assertEquals('$1,234.56', number_to_currency(1234.56, 'USD', 'en_US'));
$this->assertEquals('£1,234.56', number_to_currency(1234.56, 'GBP', 'en_GB'));
}
public function test_trillions()
{
$this->assertEquals('123.457 trillion', number_to_amount('123,456,700,000,000', 3, 'en_US'));
}
public function test_quadrillions()
{
$this->assertEquals('123.5 quadrillion', number_to_amount('123,456,700,000,000,000', 1, 'en_US'));
}
/**
* @group single
*/
public function test_currency_current_locale()
{
$this->assertEquals('$1,234.56', number_to_currency(1234.56, 'USD', 'en_US'));
$this->assertEquals('£1,234.56', number_to_currency(1234.56, 'GBP', 'en_GB'));
}
public function testNumbersThatArent()
{
$this->assertFalse(number_to_size('1232x'));
$this->assertFalse(number_to_amount('1232x'));
}
}

View File

@ -1,12 +1,15 @@
<?php namespace CodeIgniter\Helpers;
<?php
namespace CodeIgniter\Helpers;
class TextHelperTest extends \CIUnitTestCase
{
private $_long_string = 'Once upon a time, a framework had no tests. It sad. So some nice people began to write tests. The more time that went on, the happier it became. Everyone was happy.';
public function setUp()
{
parent::setUp();
parent::setUp();
helper('text');
}
@ -14,280 +17,341 @@ class TextHelperTest extends \CIUnitTestCase
// --------------------------------------------------------------------
public function test_strip_slashes()
{
$expected = [
"Is your name O'reilly?",
"No, my name is O'connor."
];
$str = [
"Is your name O\'reilly?",
"No, my name is O\'connor."
];
$this->assertEquals($expected, strip_slashes($str));
}
{
$expected = [
"Is your name O'reilly?",
"No, my name is O'connor."
];
$str = [
"Is your name O\'reilly?",
"No, my name is O\'connor."
];
$this->assertEquals($expected, strip_slashes($str));
}
// --------------------------------------------------------------------
public function test_strip_quotes()
{
$strs = [
'"me oh my!"' => 'me oh my!',
"it's a winner!" => 'its a winner!',
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, strip_quotes($str));
}
}
// --------------------------------------------------------------------
public function test_strip_quotes()
{
$strs = [
'"me oh my!"' => 'me oh my!',
"it's a winner!" => 'its a winner!',
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, strip_quotes($str));
}
}
// --------------------------------------------------------------------
public function test_quotes_to_entities()
{
$strs = [
'"me oh my!"' => '&quot;me oh my!&quot;',
"it's a winner!" => 'it&#39;s a winner!',
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, quotes_to_entities($str));
}
}
// --------------------------------------------------------------------
public function test_quotes_to_entities()
{
$strs = [
'"me oh my!"' => '&quot;me oh my!&quot;',
"it's a winner!" => 'it&#39;s a winner!',
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, quotes_to_entities($str));
}
}
// --------------------------------------------------------------------
public function test_reduce_double_slashes()
{
$strs = [
'http://codeigniter.com' => 'http://codeigniter.com',
'//var/www/html/example.com/' => '/var/www/html/example.com/',
'/var/www/html//index.php' => '/var/www/html/index.php'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, reduce_double_slashes($str));
}
}
// --------------------------------------------------------------------
public function test_reduce_double_slashes()
{
$strs = [
'http://codeigniter.com' => 'http://codeigniter.com',
'//var/www/html/example.com/' => '/var/www/html/example.com/',
'/var/www/html//index.php' => '/var/www/html/index.php'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, reduce_double_slashes($str));
}
}
// --------------------------------------------------------------------
public function test_reduce_multiples()
{
$strs = [
'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
'Ringo, John, Paul,,' => 'Ringo, John, Paul,'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, reduce_multiples($str));
}
$strs = [
'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
'Ringo, John, Paul,,' => 'Ringo, John, Paul'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, reduce_multiples($str, ',', TRUE));
}
}
// --------------------------------------------------------------------
public function test_reduce_multiples()
{
$strs = [
'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
'Ringo, John, Paul,,' => 'Ringo, John, Paul,'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, reduce_multiples($str));
}
$strs = [
'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
'Ringo, John, Paul,,' => 'Ringo, John, Paul'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, reduce_multiples($str, ',', TRUE));
}
}
// --------------------------------------------------------------------
public function test_random_string()
{
$this->assertEquals(16, strlen(random_string('alnum', 16)));
$this->assertInternalType('string', random_string('numeric', 16));
$this->assertEquals(16, strlen($random = random_string('crypto', 16)));
$this->assertInternalType('string', $random);
}
// --------------------------------------------------------------------
public function test_random_string()
{
$this->assertEquals(16, strlen(random_string('alnum', 16)));
$this->assertEquals(16, strlen(random_string('alpha', 16)));
$this->assertEquals(16, strlen(random_string('nozero', 16)));
$this->assertEquals(16, strlen(random_string('numeric', 16)));
$this->assertEquals(8, strlen(random_string('numeric')));
// --------------------------------------------------------------------
public function test_increment_string()
{
$this->assertEquals('my-test_1', increment_string('my-test'));
$this->assertEquals('my-test-1', increment_string('my-test', '-'));
$this->assertEquals('file_5', increment_string('file_4'));
$this->assertEquals('file-5', increment_string('file-4', '-'));
$this->assertEquals('file-5', increment_string('file-4', '-'));
$this->assertEquals('file-1', increment_string('file', '-', '1'));
$this->assertEquals(124, increment_string('123', ''));
}
$this->assertInternalType('string', random_string('basic'));
$this->assertEquals(16, strlen($random = random_string('crypto', 16)));
$this->assertInternalType('string', $random);
// -------------------------------------------------------------------
// Functions from text_helper_test.php
// -------------------------------------------------------------------
$this->assertEquals(32, strlen($random = random_string('md5')));
$this->assertEquals(40, strlen($random = random_string('sha1')));
}
public function test_word_limiter()
{
$this->assertEquals('Once upon a time,&#8230;', word_limiter($this->_long_string, 4));
$this->assertEquals('Once upon a time,&hellip;', word_limiter($this->_long_string, 4, '&hellip;'));
$this->assertEquals('', word_limiter('', 4));
}
// --------------------------------------------------------------------
public function test_increment_string()
{
$this->assertEquals('my-test_1', increment_string('my-test'));
$this->assertEquals('my-test-1', increment_string('my-test', '-'));
$this->assertEquals('file_5', increment_string('file_4'));
$this->assertEquals('file-5', increment_string('file-4', '-'));
$this->assertEquals('file-5', increment_string('file-4', '-'));
$this->assertEquals('file-1', increment_string('file', '-', '1'));
$this->assertEquals(124, increment_string('123', ''));
}
// ------------------------------------------------------------------------
public function test_character_limiter()
{
$this->assertEquals('Once upon a time, a&#8230;', character_limiter($this->_long_string, 20));
$this->assertEquals('Once upon a time, a&hellip;', character_limiter($this->_long_string, 20, '&hellip;'));
$this->assertEquals('Short', character_limiter('Short', 20));
$this->assertEquals('Short', character_limiter('Short', 5));
}
// -------------------------------------------------------------------
// Functions from text_helper_test.php
// -------------------------------------------------------------------
// ------------------------------------------------------------------------
public function test_ascii_to_entities()
{
$strs = [
'“‘ “test”' => '&#8220;&#8216; &#8220;test&#8221;',
'†¥¨ˆøåß∂ƒ©˙∆˚¬' => '&#8224;&#165;&#168;&#710;&#248;&#229;&#223;&#8706;&#402;&#169;&#729;&#8710;&#730;&#172;'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, ascii_to_entities($str));
}
}
public function test_word_limiter()
{
$this->assertEquals('Once upon a time,&#8230;', word_limiter($this->_long_string, 4));
$this->assertEquals('Once upon a time,&hellip;', word_limiter($this->_long_string, 4, '&hellip;'));
$this->assertEquals('', word_limiter('', 4));
$this->assertEquals('Once upon a&hellip;', word_limiter($this->_long_string, 3, '&hellip;'));
$this->assertEquals('Once upon a time', word_limiter('Once upon a time', 4, '&hellip;'));
}
// ------------------------------------------------------------------------
public function test_entities_to_ascii()
{
$strs = [
'&#8220;&#8216; &#8220;test&#8221;' => '“‘ “test”',
'&#8224;&#165;&#168;&#710;&#248;&#229;&#223;&#8706;&#402;&#169;&#729;&#8710;&#730;&#172;' => '†¥¨ˆøåß∂ƒ©˙∆˚¬'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, entities_to_ascii($str));
}
}
// ------------------------------------------------------------------------
public function test_character_limiter()
{
$this->assertEquals('Once upon a time, a&#8230;', character_limiter($this->_long_string, 20));
$this->assertEquals('Once upon a time, a&hellip;', character_limiter($this->_long_string, 20, '&hellip;'));
$this->assertEquals('Short', character_limiter('Short', 20));
$this->assertEquals('Short', character_limiter('Short', 5));
}
// ------------------------------------------------------------------------
public function test_convert_accented_characters()
{
//$this->ci_vfs_clone('application/Config/ForeignChars.php');
$this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ'));
$this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü'));
}
// ------------------------------------------------------------------------
public function test_ascii_to_entities()
{
$strs = [
'“‘ “test” ' => '&#8220;&#8216; &#8220;test&#8221; ',
'†¥¨ˆøåß∂ƒ©˙∆˚¬' => '&#8224;&#165;&#168;&#710;&#248;&#229;&#223;&#8706;&#402;&#169;&#729;&#8710;&#730;&#172;'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, ascii_to_entities($str));
}
}
// ------------------------------------------------------------------------
public function test_censored_words()
{
$censored = ['boob', 'nerd', 'ass', 'fart'];
$strs = [
'Ted bobbled the ball' => 'Ted bobbled the ball',
'Jake is a nerdo' => 'Jake is a nerdo',
'The borg will assimilate you' => 'The borg will assimilate you',
'Did Mary Fart?' => 'Did Mary $*#?',
'Jake is really a boob' => 'Jake is really a $*#'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, word_censor($str, $censored, '$*#'));
}
// test censored words being sent as a string
$this->assertEquals('test', word_censor('test', 'test'));
}
// ------------------------------------------------------------------------
public function test_entities_to_ascii()
{
$strs = [
'&#8220;&#8216; &#8220;test&#8221; ' => '“‘ “test” ',
'&#8224;&#165;&#168;&#710;&#248;&#229;&#223;&#8706;&#402;&#169;&#729;&#8710;&#730;&#172;' => '†¥¨ˆøåß∂ƒ©˙∆˚¬'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, entities_to_ascii($str));
}
}
// ------------------------------------------------------------------------
public function test_highlight_code()
{
$expect = "<code><span style=\"color: #000000\">\n<span style=\"color: #0000BB\">&lt;?php&nbsp;var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">);&nbsp;</span><span style=\"color: #0000BB\">?&gt;&nbsp;</span>\n</span>\n</code>";
$this->assertEquals($expect, highlight_code('<?php var_dump($this); ?>'));
}
public function testEntitiesToAsciiUnsafe()
{
$str = '&lt;&gt;';
$this->assertEquals('<>', entities_to_ascii($str, true));
$this->assertEquals('&lt;&gt;', entities_to_ascii($str, false));
}
// ------------------------------------------------------------------------
public function test_highlight_phrase()
{
$strs = [
'this is a phrase' => '<mark>this is</mark> a phrase',
'this is another' => '<mark>this is</mark> another',
'Gimme a test, Sally' => 'Gimme a test, Sally',
'Or tell me what this is' => 'Or tell me what <mark>this is</mark>',
'' => ''
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, highlight_phrase($str, 'this is'));
}
$this->assertEquals('<strong>this is</strong> a strong test', highlight_phrase('this is a strong test', 'this is', '<strong>', '</strong>'));
}
public function testEntitiesToAsciiSmallOrdinals()
{
$str = '&#07;';
$this->assertEquals(pack('c', 7), entities_to_ascii($str));
}
// ------------------------------------------------------------------------
public function test_ellipsize()
{
$strs = [
'0' => [
'this is my string' => '&hellip; my string',
"here's another one" => '&hellip;nother one',
'this one is just a bit longer' => '&hellip;bit longer',
'short' => 'short'
],
'.5' => [
'this is my string' => 'this &hellip;tring',
"here's another one" => "here'&hellip;r one",
'this one is just a bit longer' => 'this &hellip;onger',
'short' => 'short'
],
'1' => [
'this is my string' => 'this is my&hellip;',
"here's another one" => "here's ano&hellip;",
'this one is just a bit longer' => 'this one i&hellip;',
'short' => 'short'
],
];
foreach ($strs as $pos => $s)
{
foreach ($s as $str => $expect)
{
$this->assertEquals($expect, ellipsize($str, 10, $pos));
}
}
}
// ------------------------------------------------------------------------
public function test_convert_accented_characters()
{
//$this->ci_vfs_clone('application/Config/ForeignChars.php');
$this->assertEquals('AAAeEEEIIOOEUUUeY', convert_accented_characters('ÀÂÄÈÊËÎÏÔŒÙÛÜŸ'));
$this->assertEquals('a e i o u n ue', convert_accented_characters('á é í ó ú ñ ü'));
}
// ------------------------------------------------------------------------
public function test_word_wrap()
{
$string = 'Here is a simple string of text that will help us demonstrate this function.';
$this->assertEquals(substr_count(word_wrap($string, 25), "\n"), 4);
}
// ------------------------------------------------------------------------
public function test_censored_words()
{
$censored = ['boob', 'nerd', 'ass', 'fart'];
$strs = [
'Ted bobbled the ball' => 'Ted bobbled the ball',
'Jake is a nerdo' => 'Jake is a nerdo',
'The borg will assimilate you' => 'The borg will assimilate you',
'Did Mary Fart?' => 'Did Mary $*#?',
'Jake is really a boob' => 'Jake is really a $*#',
'Jake is really a (boob)' => 'Jake is really a ($*#)'
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, word_censor($str, $censored, '$*#'));
}
// test censored words being sent as a string
$this->assertEquals('this is a test', word_censor('this is a test', 'test'));
}
// ------------------------------------------------------------------------
public function test_default_word_wrap_charlim()
{
$string = "Here is a longer string of text that will help us demonstrate the default charlim of this function.";
$this->assertEquals(strpos(word_wrap($string), "\n"), 73);
}
// ------------------------------------------------------------------------
public function test_highlight_code()
{
$expect = "<code><span style=\"color: #000000\">\n<span style=\"color: #0000BB\">&lt;?php&nbsp;var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">);&nbsp;</span><span style=\"color: #0000BB\">?&gt;&nbsp;</span>\n</span>\n</code>";
$this->assertEquals($expect, highlight_code('<?php var_dump($this); ?>'));
}
// -----------------------------------------------------------------------
// ------------------------------------------------------------------------
public function test_highlight_phrase()
{
$strs = [
'this is a phrase' => '<mark>this is</mark> a phrase',
'this is another' => '<mark>this is</mark> another',
'Gimme a test, Sally' => 'Gimme a test, Sally',
'Or tell me what this is' => 'Or tell me what <mark>this is</mark>',
'' => ''
];
foreach ($strs as $str => $expect)
{
$this->assertEquals($expect, highlight_phrase($str, 'this is'));
}
$this->assertEquals('<strong>this is</strong> a strong test', highlight_phrase('this is a strong test', 'this is', '<strong>', '</strong>'));
}
public function test_excerpt()
{
$string = $this->_long_string;
$result = ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...';
$this->assertEquals(excerpt($string), $result);
}
// ------------------------------------------------------------------------
public function test_ellipsize()
{
$strs = [
'0' => [
'this is my string' => '&hellip; my string',
"here's another one" => '&hellip;nother one',
'this one is just a bit longer' => '&hellip;bit longer',
'short' => 'short'
],
'.5' => [
'this is my string' => 'this &hellip;tring',
"here's another one" => "here'&hellip;r one",
'this one is just a bit longer' => 'this &hellip;onger',
'short' => 'short'
],
'1' => [
'this is my string' => 'this is my&hellip;',
"here's another one" => "here's ano&hellip;",
'this one is just a bit longer' => 'this one i&hellip;',
'short' => 'short'
],
];
foreach ($strs as $pos => $s)
{
foreach ($s as $str => $expect)
{
$this->assertEquals($expect, ellipsize($str, 10, $pos));
}
}
}
// -----------------------------------------------------------------------
// ------------------------------------------------------------------------
public function testWordWrap()
{
$string = 'Here is a simple string of text that will help us demonstrate this function.';
$expected = "Here is a simple string\nof text that will help us\ndemonstrate this\nfunction.";
$this->assertEquals(substr_count(word_wrap($string, 25), "\n"), 3);
$this->assertEquals($expected, word_wrap($string, 25));
public function test_excerpt_radius()
{
$string = $this->_long_string;
$phrase = 'began';
$result = '... people began to ...';
$this->assertEquals(excerpt($string, $phrase, 10), $result);
}
$string2 = "Here is a\nbroken up sentence\rspanning lines\r\nwoohoo!";
$expected2 = "Here is a\nbroken up sentence\nspanning lines\nwoohoo!";
$this->assertEquals(substr_count(word_wrap($string2, 25), "\n"), 3);
$this->assertEquals($expected2, word_wrap($string2, 25));
// -----------------------------------------------------------------------
$string3 = "Here is another slightly longer\nbroken up sentence\rspanning lines\r\nwoohoo!";
$expected3 = "Here is another slightly\nlonger\nbroken up sentence\nspanning lines\nwoohoo!";
$this->assertEquals(substr_count(word_wrap($string3, 25), "\n"), 4);
$this->assertEquals($expected3, word_wrap($string3, 25));
}
public function test_alternator()
{
$phrase = ' scream! ';
$result = '';
alternator();
for ($i = 0; $i < 4; $i ++ )
$result .= alternator('I', 'you', 'we') . $phrase;
$this->assertEquals('I scream! you scream! we scream! I scream! ', $result);
}
public function testWordWrapUnwrap()
{
$string = 'Here is a {unwrap}simple string of text{/unwrap} that will help us demonstrate this function.';
$expected = "Here is a simple string of text\nthat will help us\ndemonstrate this\nfunction.";
$this->assertEquals(substr_count(word_wrap($string, 25), "\n"), 3);
$this->assertEquals($expected, word_wrap($string, 25));
}
public function testWordWrapLongWords()
{
// the really really long word will be split
$string = 'Here is an unbelievable super-complicated and reallyreallyquiteextraordinarily sophisticated sentence.';
$expected = "Here is an unbelievable\nsuper-complicated and\nreallyreallyquiteextraor\ndinarily\nsophisticated sentence.";
$this->assertEquals($expected, word_wrap($string, 25));
}
public function testWordWrapURL()
{
// the really really long word will be split
$string = 'Here is an unbelievable super-complicated and http://www.reallyreallyquiteextraordinarily.com sophisticated sentence.';
$expected = "Here is an unbelievable\nsuper-complicated and\nhttp://www.reallyreallyquiteextraordinarily.com\nsophisticated sentence.";
$this->assertEquals($expected, word_wrap($string, 25));
}
// ------------------------------------------------------------------------
public function test_default_word_wrap_charlim()
{
$string = "Here is a longer string of text that will help us demonstrate the default charlim of this function.";
$this->assertEquals(strpos(word_wrap($string), "\n"), 73);
}
// -----------------------------------------------------------------------
public function test_excerpt()
{
$string = $this->_long_string;
$result = ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...';
$this->assertEquals(excerpt($string), $result);
}
// -----------------------------------------------------------------------
public function test_excerpt_radius()
{
$string = $this->_long_string;
$phrase = 'began';
$result = '... people began to ...';
$this->assertEquals(excerpt($string, $phrase, 10), $result);
}
// -----------------------------------------------------------------------
public function test_alternator()
{
$phrase = ' scream! ';
$result = '';
alternator();
for ($i = 0; $i < 4; $i ++ )
$result .= alternator('I', 'you', 'we') . $phrase;
$this->assertEquals('I scream! you scream! we scream! I scream! ', $result);
}
public function test_empty_alternator()
{
$phrase = ' scream! ';
$result = '';
for ($i = 0; $i < 4; $i ++ )
$result .= alternator() . $phrase;
$this->assertEquals(' scream! scream! scream! scream! ', $result);
}
public function test_empty_alternator()
{
$phrase = ' scream! ';
$result = '';
for ($i = 0; $i < 4; $i ++ )
$result .= alternator() . $phrase;
$this->assertEquals(' scream! scream! scream! scream! ', $result);
}
}

View File

@ -187,8 +187,6 @@ class URLHelperTest extends \CIUnitTestCase
$this->assertEquals('http://example.com/index.php/', site_url());
}
//--------------------------------------------------------------------
/**
* @see https://github.com/bcit-ci/CodeIgniter4/issues/240
*/
@ -209,8 +207,6 @@ class URLHelperTest extends \CIUnitTestCase
$this->assertEquals('http://example.com/index.php/profile', site_url('profile'));
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Test base_url
@ -289,8 +285,6 @@ class URLHelperTest extends \CIUnitTestCase
$this->assertEquals('http://example.com/', base_url());
}
//--------------------------------------------------------------------
/**
* @see https://github.com/bcit-ci/CodeIgniter4/issues/867
*/
@ -323,8 +317,6 @@ class URLHelperTest extends \CIUnitTestCase
$this->assertEquals('http://example.com/profile', base_url('profile'));
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Test current_url
@ -856,6 +848,8 @@ class URLHelperTest extends \CIUnitTestCase
{
$this->assertEquals('http://codeigniter.com', prep_url('codeigniter.com'));
$this->assertEquals('http://www.codeigniter.com', prep_url('www.codeigniter.com'));
$this->assertEquals('', prep_url());
$this->assertEquals('http://www.codeigniter.com', prep_url('http://www.codeigniter.com'));
}
//--------------------------------------------------------------------

View File

@ -16,6 +16,19 @@ class FeatureTestCaseTest extends FeatureTestCase
$this->skipEvents();
}
public function testCallGet()
{
$this->withRoutes([
['get', 'home', function() {
return 'Hello World';
}]
]);
$response = $this->get('home');
$response->assertSee('Hello World');
$response->assertDontSee('Again');
}
public function testCallSimpleGet()
{
$this->withRoutes([
@ -32,19 +45,6 @@ class FeatureTestCaseTest extends FeatureTestCase
$this->assertEquals(200, $response->response->getStatusCode());
}
public function testCallGet()
{
$this->withRoutes([
['get', 'home', function() {
return 'Hello World';
}]
]);
$response = $this->get('home');
$response->assertSee('Hello World');
$response->assertDontSee('Again');
}
public function testCallPost()
{
$this->withRoutes([

View File

@ -40,9 +40,10 @@ characters so that it can be used safely::
<input type="text" name="myfield" value="<?= esc($string); ?>" />
.. note:: If you use any of the form helper functions listed on this page,
and you pass values as an associative array,
the form values will be automatically escaped, so there is no need
to call this function. Use it only if you are creating your own
form elements.
form elements, which you would pass as strings.
Available Functions
===================
@ -52,7 +53,7 @@ The following functions are available:
.. php:function:: form_open([$action = ''[, $attributes = ''[, $hidden = array()]]])
:param string $action: Form action/target URI string
:param array $attributes: HTML attributes
:param mixed $attributes: HTML attributes, as an array or escaped string
:param array $hidden: An array of hidden fields' definitions
:returns: An HTML form opening tag
:rtype: string
@ -106,15 +107,15 @@ The following functions are available:
<input type="hidden" name="username" value="Joe" />
<input type="hidden" name="member_id" value="234" />
.. php:function:: form_open_multipart([$action = ''[, $attributes = array()[, $hidden = array()]]])
.. php:function:: form_open_multipart([$action = ''[, $attributes = ''[, $hidden = array()]]])
:param string $action: Form action/target URI string
:param array $attributes: HTML attributes
:param mixed $attributes: HTML attributes, as an array or escaped string
:param array $hidden: An array of hidden fields' definitions
:returns: An HTML multipart form opening tag
:rtype: string
This function is absolutely identical to :php:func:`form_open()` above,
This function is identical to :php:func:`form_open()` above,
except that it adds a *multipart* attribute, which is necessary if you
would like to use the form to upload files with.
@ -289,7 +290,7 @@ The following functions are available:
contain the name of the field, the second parameter will contain an
associative array of options, and the third parameter will contain the
value you wish to be selected. You can also pass an array of multiple
items through the third parameter, and CodeIgniter will create a
items through the third parameter, and the helper will create a
multiple select for you.
Example::

View File

@ -26,9 +26,9 @@ The following functions are available:
.. php:function:: img([$src = ''[, $indexPage = false[, $attributes = '']]])
:param string $src: Image source data
:param mixed $src: Image source data
:param bool $indexPage: Whether to treat $src as a routed URI string
:param array $attributes: HTML attributes
:param mixed $attributes: HTML attributes
:returns: HTML image tag
:rtype: string
@ -46,7 +46,7 @@ The following functions are available:
echo img('images/picture.jpg', true);
// <img src="http://site.com/index.php/images/picture.jpg" alt="" />
Additionally, an associative array can be passed to the ``img()`` function
Additionally, an associative array can be passed as the first parameter,
for complete control over all attributes and values. If an *alt* attribute
is not provided, CodeIgniter will generate an empty string.
@ -96,7 +96,7 @@ The following functions are available:
echo link_tag('feed', 'alternate', 'application/rss+xml', 'My RSS Feed');
// <link href="http://site.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" />
Additionally, an associative array can be passed to the ``link()`` function
Alternately, an associative array can be passed to the ``link_tag()`` function
for complete control over all attributes and values::
$link = array(
@ -111,7 +111,7 @@ The following functions are available:
.. php:function:: script_tag([$src = ''[, $indexPage = false]])
:param string $src: The source of JavaScript file
:param mixed $src: The source name of a JavaScript file
:param bool $indexPage: Whether to treat $src as a routed URI string
:returns: HTML script tag
:rtype: string
@ -126,11 +126,7 @@ The following functions are available:
echo script_tag('js/mystyles.js');
// <script src="http://site.com/js/mystyles.js" type="text/javascript"></script>
Further examples::
// to be done
Additionally, an associative array can be passed to the ``script_tag()`` function
Alternately, an associative array can be passed to the ``script_tag()`` function
for complete control over all attributes and values::
$script = array('src' => 'js/printer.js');
@ -341,7 +337,7 @@ The following functions are available:
:param string $attributes:
:param array $tracks: Use the track function inside an array. See :php:func:`track()` function
:param bool $indexPage:
:returns: HTML-formatted video element
:returns: HTML-formatted audio element
:rtype: string
Identical to :php:func:`video()`, only it produces the <audio> tag instead of <video>.
@ -456,8 +452,8 @@ The following functions are available:
echo doctype('html4-trans');
// <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
The following is a list of doctype choices. These are configurable, and
pulled from application/config/doctypes.php
The following is a list of the pre-defined doctype choices. These are configurable,
pulled from `application/Config/DocTypes.php`, or they could be over-ridden in your `.env` configuration.
=============================== =================== ==================================================================================================================================================
Document type Option Result

View File

@ -19,6 +19,13 @@ This helper is loaded using the following code::
helper('number');
When Things Go Wrong
====================
If PHP's internationalization and localization logic cannot handle
a value provided, for the given locale and options, then a
``BadFunctionCallException()`` will be thrown.
Available Functions
===================
@ -28,7 +35,7 @@ The following functions are available:
:param mixed $num: Number of bytes
:param int $precision: Floating point precision
:returns: Formatted data size string
:returns: Formatted data size string, or false if the provided value is not numeric
:rtype: string
Formats numbers as bytes, based on size, and adds the appropriate
@ -64,7 +71,7 @@ The following functions are available:
:param mixed $num: Number to format
:param int $precision: Floating point precision
:param string $locale: The locale to use for formatting
:returns: A human-readable version of the string
:returns: A human-readable version of the string, or false if the provided value is not numeric
:rtype: string
Converts a number into a human-readable version, like **123.4 trillion**
@ -109,3 +116,6 @@ The following functions are available:
echo number_to_roman(23); // Returns XXIII
echo number_to_roman(324); // Returns CCCXXIV
echo number_to_roman(2534); // Returns MMDXXXIV
This function only handles numbers in the range 1 through 3999.
It will return null for any value outside that range .

View File

@ -38,7 +38,7 @@ The following functions are available:
- **alpha**: A string with lower and uppercase letters only.
- **alnum**: Alpha-numeric string with lower and uppercase characters.
- **basic**: A random number based on ``mt_rand()``.
- **basic**: A random number based on ``mt_rand()`` (length ignored).
- **numeric**: Numeric string.
- **nozero**: Numeric string with no zeros.
- **md5**: An encrypted random number based on ``md5()`` (fixed length of 32).
@ -352,6 +352,8 @@ The following functions are available:
// demonstrate this
// function.
Excessively long words will be split, but URLs will not be.
.. php:function:: ellipsize($str, $max_length[, $position = 1[, $ellipsis = '&hellip;']])
:param string $str: Input string

View File

@ -12,7 +12,9 @@ and :doc:`images </libraries/images>`.
Getting a File instance
=======================
You create a new File instance by passing in the path to the file in the constructor. By default the file does not need to exist. However, you can pass an additional argument of "true" to check that the file exist and throw ``FileNotFoundException()`` when it does not.
You create a new File instance by passing in the path to the file in the constructor.
By default the file does not need to exist. However, you can pass an additional argument of "true"
to check that the file exist and throw ``FileNotFoundException()`` if it does not.
::