Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix appendTitle, prependTitle. Update unit test for append, prepend and setTitle. #12236

Closed
wants to merge 14 commits into from
Closed
50 changes: 42 additions & 8 deletions phalcon/tag.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1061,17 +1061,51 @@ class Tag
/**
* Appends a text to current document title
*/
public static function appendTitle(string title) -> void
public static function appendTitle(var title) -> void
{
let self::_documentAppendTitle = title;
var append = [];
if self::_documentAppendTitle !== null {
let append = self::_documentAppendTitle ;
}

if typeof title == "array" {
let append = title;
} else {
if typeof title == "string" {
let append[] = title;
}
}

if empty append {
let append = null ;
}

let self::_documentAppendTitle = append ;
}

/**
* Prepends a text to current document title
*/
public static function prependTitle(string title) -> void
public static function prependTitle(var title) -> void
{
let self::_documentPrependTitle = title;
var prepend = [];
if self::_documentPrependTitle !== null {
let prepend = self::_documentPrependTitle ;
}

if typeof title == "array" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new feature request?

let prepend = title;
} else {
if typeof title == "string" {
array_unshift(prepend, title) ;
}
}

if empty prepend {
let prepend = null ;
}

let self::_documentPrependTitle = prepend ;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now _documentPrependTitle is array or null and this breaking BC

}

/**
Expand All @@ -1093,21 +1127,21 @@ class Tag
let escaper = <EscaperInterface> self::getEscaper(["escape": true]);
let items = [];
let output = "";
let documentPrependTitle = escaper->escapeHtml(self::_documentPrependTitle);
let documentPrependTitle = self::_documentPrependTitle;
let documentTitle = escaper->escapeHtml(self::_documentTitle);
let documentAppendTitle = escaper->escapeHtml(self::_documentAppendTitle);
let documentAppendTitle = self::_documentAppendTitle;
let documentTitleSeparator = escaper->escapeHtml(self::_documentTitleSeparator);

if !empty documentPrependTitle {
let items[] = documentPrependTitle;
let items[] = escaper->escapeHtml(implode(documentTitleSeparator, documentPrependTitle));
}

if !empty documentTitle {
let items[] = documentTitle;
}

if !empty documentAppendTitle {
let items[] = documentAppendTitle;
let items[] = escaper->escapeHtml(implode(documentTitleSeparator, documentAppendTitle));
}

if empty documentTitleSeparator {
Expand Down
32 changes: 28 additions & 4 deletions tests/unit/Tag/TagTitleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ function () {
Tag::setTitle($value);

expect(Tag::getTitle())->equals("<title>{$value}</title>" . PHP_EOL);

expect(Tag::getTitle(false))->equals("{$value}");
}
);
}
Expand All @@ -84,19 +86,30 @@ function () {
Tag::appendTitle('Class');

expect(Tag::getTitle())->equals("<title>TitleClass</title>" . PHP_EOL);


Tag::resetInput();

Tag::setTitle('This is my title');
Tag::appendTitle(' - Welcome!');

expect(Tag::getTitle())->equals("<title>This is my title - Welcome!</title>" . PHP_EOL);


Tag::resetInput();

Tag::setTitle('Title');
Tag::setTitleSeparator('|');
Tag::appendTitle('Class');

expect(Tag::getTitle())->equals("<title>Title|Class</title>" . PHP_EOL);

Tag::resetInput();

Tag::setTitle('Main');
Tag::setTitleSeparator(' - ');
Tag::appendTitle('Category');
Tag::appendTitle('Title');

expect(Tag::getTitle())->equals("<title>Main - Category - Title</title>" . PHP_EOL);
}
);
}
Expand All @@ -119,12 +132,23 @@ function () {
Tag::prependTitle('PhalconPHP - ');

expect(Tag::getTitle())->equals("<title>PhalconPHP - This is my title</title>" . PHP_EOL);


Tag::resetInput();

Tag::setTitle('Title');
Tag::setTitleSeparator('|');
Tag::prependTitle('Class');

expect(Tag::getTitle())->equals('<title>Class|Title</title>' . PHP_EOL);

Tag::resetInput();

Tag::setTitle('Main');
Tag::setTitleSeparator(' - ');
Tag::prependTitle('Category');
Tag::prependTitle('Title');

expect(Tag::getTitle())->equals("<title>Title - Category - Main</title>" . PHP_EOL);
}
);
}
Expand Down