From 51710d37e93cdb80572be3460f0477131fd98ad6 Mon Sep 17 00:00:00 2001 From: Matthias Gutjahr Date: Fri, 25 Apr 2014 10:12:17 +0200 Subject: [PATCH] Complete refactoring of serendipity_event_typesetbuttons - Add new syntax for s9y 2.0 - Add button classes - Add unit tests --- .../buttons/AccentButton.php | 36 ++ .../buttons/AmpButton.php | 36 ++ .../buttons/AposButton.php | 73 +++ .../buttons/BulletButton.php | 40 ++ .../buttons/Button.php | 265 +++++++++ .../buttons/ButtonInterface.php | 41 ++ .../buttons/CenterButton.php | 42 ++ .../buttons/CustomButton.php | 70 +++ .../buttons/DquotesButton.php | 137 +++++ .../buttons/EmdashButton.php | 40 ++ .../buttons/EndashButton.php | 40 ++ .../buttons/GaccentButton.php | 38 ++ .../buttons/SpaceButton.php | 38 ++ .../buttons/SquotesButton.php | 137 +++++ .../buttons/StrikeButton.php | 46 ++ .../serendipity_event_typesetbuttons.php | 523 ++++++------------ .../tests/buttons/AccentButtonTest.php | 40 ++ .../tests/buttons/AmpButtonTest.php | 40 ++ .../tests/buttons/AposButtonTest.php | 63 +++ .../tests/buttons/BulletButtonTest.php | 52 ++ .../tests/buttons/CenterButtonTest.php | 51 ++ .../tests/buttons/CustomButtonTest.php | 67 +++ .../tests/buttons/DquotesButtonTest.php | 81 +++ .../tests/buttons/EmdashButtonTest.php | 57 ++ .../tests/buttons/EndashButtonTest.php | 57 ++ .../tests/buttons/GaccentButtonTest.php | 40 ++ .../tests/buttons/SpaceButtonTest.php | 40 ++ .../tests/buttons/SquotesButtonTest.php | 80 +++ .../tests/buttons/StrikeButtonTest.php | 51 ++ .../tests/typesetbuttonsTest.php | 349 ++++++++++++ 30 files changed, 2320 insertions(+), 350 deletions(-) create mode 100644 serendipity_event_typesetbuttons/buttons/AccentButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/AmpButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/AposButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/BulletButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/Button.php create mode 100644 serendipity_event_typesetbuttons/buttons/ButtonInterface.php create mode 100644 serendipity_event_typesetbuttons/buttons/CenterButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/CustomButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/DquotesButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/EmdashButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/EndashButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/GaccentButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/SpaceButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/SquotesButton.php create mode 100644 serendipity_event_typesetbuttons/buttons/StrikeButton.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/AccentButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/AmpButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/AposButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/BulletButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/CenterButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/CustomButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/DquotesButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/EmdashButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/EndashButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/GaccentButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/SpaceButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/SquotesButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/buttons/StrikeButtonTest.php create mode 100644 serendipity_event_typesetbuttons/tests/typesetbuttonsTest.php diff --git a/serendipity_event_typesetbuttons/buttons/AccentButton.php b/serendipity_event_typesetbuttons/buttons/AccentButton.php new file mode 100644 index 00000000..79bc0f8f --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/AccentButton.php @@ -0,0 +1,36 @@ +setName('insaccent'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_ACCENT_BUTTON); + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&\\#x0301\\;','')"); + } else { + $this->addClass('wrap_selection'); + $this->setOpenTag('́'); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/AmpButton.php b/serendipity_event_typesetbuttons/buttons/AmpButton.php new file mode 100644 index 00000000..b6f0b03a --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/AmpButton.php @@ -0,0 +1,36 @@ +setName('insamp'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_AMP_BUTTON); + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&\\#38\\;','')"); + } else { + $this->addClass('wrap_selection'); + $this->setOpenTag('&'); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/AposButton.php b/serendipity_event_typesetbuttons/buttons/AposButton.php new file mode 100644 index 00000000..dd191564 --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/AposButton.php @@ -0,0 +1,73 @@ +setName('insapos'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_APOS_BUTTON); + } + + /** + * @return boolean + */ + public function isUseRealApos() + { + return $this->useRealApos; + } + + /** + * @param boolean $useRealApos + */ + public function setUseRealApos($useRealApos) + { + $this->useRealApos = $useRealApos; + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + if ($this->isUseRealApos() === false) { + if ($this->isUseNamedEnts()) { + $this->setOnClickEvent( + "wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea( + ) . "'],'’','')" + ); + } else { + $this->setOnClickEvent( + "wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea( + ) . "'],'’','')" + ); + } + } else { + $this->setOnClickEvent( + "wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&\\#39\\;','')" + ); + } + } else { + $this->addClass('wrap_selection'); + $this->setOpenTag('''); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/BulletButton.php b/serendipity_event_typesetbuttons/buttons/BulletButton.php new file mode 100644 index 00000000..a34420f9 --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/BulletButton.php @@ -0,0 +1,40 @@ +setName('insbull'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_BULLET_BUTTON); + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + if ($this->isUseNamedEnts()) { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&bull\\;','')"); + } else { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&\\#8226\\;','')"); + } + } else { + $this->addClass('wrap_selection'); + $this->setOpenTag('•'); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/Button.php b/serendipity_event_typesetbuttons/buttons/Button.php new file mode 100644 index 00000000..5ec318ff --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/Button.php @@ -0,0 +1,265 @@ +setTextarea($textarea); + } + + /** + * @return string + */ + public function getTextarea() + { + return $this->textarea; + } + + /** + * @param string $textarea + */ + public function setTextarea($textarea) + { + $this->textarea = $textarea; + } + + /** + * @return array + */ + public function getClasses() + { + return $this->classes; + } + + /** + * @param array $classes + */ + public function setClasses($classes) + { + $this->classes = $classes; + } + + /** + * @param string $class + */ + public function addClass($class) + { + if (!in_array($class, $this->getClasses())) { + $this->classes[] = $class; + } + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @return string + */ + public function getOnClickEvent() + { + return $this->onClickEvent; + } + + /** + * @param string $onClickEvent + */ + public function setOnClickEvent($onClickEvent) + { + $this->onClickEvent = $onClickEvent; + } + + /** + * @return string + */ + public function getValue() + { + return $this->value; + } + + /** + * @param string $value + */ + public function setValue($value) + { + $this->value = $value; + } + + /** + * @return boolean + */ + public function isXhtml11() + { + return $this->isXhtml11; + } + + /** + * @param boolean $isXhtml11 + */ + public function setIsXhtml11($isXhtml11) + { + $this->isXhtml11 = $isXhtml11; + } + + /** + * @return boolean + */ + public function isUseNamedEnts() + { + return $this->useNamedEnts; + } + + /** + * @param boolean $useNamedEnts + */ + public function setUseNamedEnts($useNamedEnts) + { + $this->useNamedEnts = $useNamedEnts; + } + + /** + * @return boolean + */ + public function isLegacyMode() + { + return $this->isLegacyMode; + } + + /** + * @param boolean $isLegayMode + */ + public function setIsLegacyMode($isLegayMode) + { + $this->isLegacyMode = $isLegayMode; + } + + /** + * @return string + */ + public function getCloseTag() + { + return $this->closeTag; + } + + /** + * @param string $closeTag + */ + public function setCloseTag($closeTag) + { + $this->closeTag = $closeTag; + } + + /** + * @return string + */ + public function getOpenTag() + { + return $this->openTag; + } + + /** + * @param string $openTag + */ + public function setOpenTag($openTag) + { + $this->openTag = $openTag; + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $html = sprintf( + '', + implode(' ', $this->getClasses()), + $this->getName(), + $this->getValue(), + $this->getOnClickEvent() + ); + } else { + $html = sprintf( + '', + implode(' ', $this->getClasses()), + $this->getName(), + $this->getOpenTag(), + $this->getCloseTag(), + $this->getTextarea(), + $this->getValue() + ); + } + return ' ' . $html . PHP_EOL; + } +} diff --git a/serendipity_event_typesetbuttons/buttons/ButtonInterface.php b/serendipity_event_typesetbuttons/buttons/ButtonInterface.php new file mode 100644 index 00000000..25db1981 --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/ButtonInterface.php @@ -0,0 +1,41 @@ +setName('inscenter'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_CENTER_BUTTON); + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + if ($this->isXhtml11()) { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'
','
')"); + } else { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'
','
')"); + } + } else { + $this->addClass('wrap_selection'); + $this->addClass('lang-html'); + $this->setOpenTag('p style=\'text-align: center;\''); + $this->setCloseTag('p'); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/CustomButton.php b/serendipity_event_typesetbuttons/buttons/CustomButton.php new file mode 100644 index 00000000..532b2756 --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/CustomButton.php @@ -0,0 +1,70 @@ +close; + } + + /** + * @param string $close + */ + public function setClose($close) + { + $this->close = $close; + } + + /** + * @return string + */ + public function getOpen() + { + return $this->open; + } + + /** + * @param string $open + */ + public function setOpen($open) + { + $this->open = $open; + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + $this->setOnClickEvent( + "wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'], '" . $this->getOpen( + ) . "', '" . $this->getClose() . "')" + ); + } else { + $this->addClass('wrap_selection'); + $this->setOpenTag($this->getOpen()); + $this->setCloseTag($this->getClose()); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/DquotesButton.php b/serendipity_event_typesetbuttons/buttons/DquotesButton.php new file mode 100644 index 00000000..3f3d098a --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/DquotesButton.php @@ -0,0 +1,137 @@ +setName('insdquote'); + } + + /** + * @return string + */ + public function render() + { + $this->overwriteValue(); + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + if ($this->isUseNamedEnts()) { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "']," . $this->getSurroundingNamedEntitiesStringByType() . ")"); + } else { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "']," . $this->getSurroundingStringByType() . ")"); + } + } else { + $this->addClass('wrap_selection'); + $namedEntities = $this->getCleanSurroundingStringByType(); + $tags = explode(',', $namedEntities); + $this->setOpenTag(trim($tags[0], '\'')); + $this->setCloseTag(trim($tags[1], '\'')); + } + return parent::render(); + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @param string $type + */ + public function setType($type) + { + $this->type = $type; + } + + /** + * Overwrite the value property according to given type + */ + private function overwriteValue() + { + $typeNumber = (int) preg_replace('$type$', '', $this->getType()); + $constName = 'PLUGIN_EVENT_TYPESETBUTTONS_DBQUOTES' . $typeNumber . '_BUTTON'; + parent::setValue(constant($constName)); + } + + /** + * @return string + */ + private function getSurroundingStringByType() + { + $surroundingStrings = array( + 'type1' => "'\\&\\#8220\\;','\\&\\#8221\\;'", + 'type2' => "'\\&\\#8222\\;','\\&\\#8220\\;'", + 'type3' => "'\\&\\#8222\\;','\\&\\#8221\\;'", + 'type4' => "'\\&\\#8221\\;','\\&\\#8221\\;'", + 'type5' => "'\\&\\#8220\\;','\\&\\#8222\\;'", + 'type6' => "'\\&\\#171\\;\\&\\#160\\;','\\&\\#160\\;\\&\\#187\\;'", + 'type7' => "'\\&\\#187\\;','\\&\\#171\\;'", + 'type8' => "'\\&\\#187\\;','\\&\\#187\\;'", + ); + if (!array_key_exists($this->getType(), $surroundingStrings)) { + return $surroundingStrings['type1']; + } + return $surroundingStrings[$this->getType()]; + } + + /** + * @return string + */ + private function getSurroundingNamedEntitiesStringByType() + { + $surroundingStrings = array( + 'type1' => "'\\&ldquo\\;','\\&rdquo\\;'", + 'type2' => "'\\&bdquo\\;','\\&ldquo\\;'", + 'type3' => "'\\&bdquo\\;','\\&rdquo\\;'", + 'type4' => "'\\&rdquo\\;','\\&rdquo\\;'", + 'type5' => "'\\&ldquo\\;','\\&bdquo\\;'", + 'type6' => "'\\&\\#171\\;\\&\\#160\\;','\\&\\#160\\;\\&\\#187\\;'", + 'type7' => "'\\&\\#187\\;','\\&\\#171\\;'", + 'type8' => "'\\&\\#187\\;','\\&\\#187\\;'", + ); + if (!array_key_exists($this->getType(), $surroundingStrings)) { + return $surroundingStrings['type1']; + } + return $surroundingStrings[$this->getType()]; + } + + /** + * @return string + */ + private function getCleanSurroundingStringByType() + { + $surroundingStrings = array( + 'type1' => "'“','”'", + 'type2' => "'„','“'", + 'type3' => "'„','”'", + 'type4' => "'”','”'", + 'type5' => "'“','„'", + 'type6' => "'« ',' »'", + 'type7' => "'»','«'", + 'type8' => "'»','»'", + ); + if (!array_key_exists($this->getType(), $surroundingStrings)) { + return $surroundingStrings['type1']; + } + return $surroundingStrings[$this->getType()]; + } +} diff --git a/serendipity_event_typesetbuttons/buttons/EmdashButton.php b/serendipity_event_typesetbuttons/buttons/EmdashButton.php new file mode 100644 index 00000000..57af9be0 --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/EmdashButton.php @@ -0,0 +1,40 @@ +setName('insemd'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_EMDASH_BUTTON); + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + if ($this->isUseNamedEnts()) { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&mdash\\;','')"); + } else { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&\\#8212\\;','')"); + } + } else { + $this->addClass('wrap_selection'); + $this->setOpenTag('—'); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/EndashButton.php b/serendipity_event_typesetbuttons/buttons/EndashButton.php new file mode 100644 index 00000000..5c3dac88 --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/EndashButton.php @@ -0,0 +1,40 @@ +setName('insend'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_ENDASH_BUTTON); + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + if ($this->isUseNamedEnts()) { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&ndash\\;','')"); + } else { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&\\#8211\\;','')"); + } + } else { + $this->addClass('wrap_selection'); + $this->setOpenTag('–'); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/GaccentButton.php b/serendipity_event_typesetbuttons/buttons/GaccentButton.php new file mode 100644 index 00000000..78b77106 --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/GaccentButton.php @@ -0,0 +1,38 @@ +setName('insgaccent'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_GACCENT_BUTTON); + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + $this->setOnClickEvent( + "wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&\\#x0300\\;','')" + ); + } else { + $this->addClass('wrap_selection'); + $this->setOpenTag('̀'); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/SpaceButton.php b/serendipity_event_typesetbuttons/buttons/SpaceButton.php new file mode 100644 index 00000000..9b9c462b --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/SpaceButton.php @@ -0,0 +1,38 @@ +setName('insSpace'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_SPACE_BUTTON); + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + $this->setOnClickEvent( + "wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'\\&\\#160\\;','')" + ); + } else { + $this->addClass('wrap_selection'); + $this->setOpenTag(' '); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/buttons/SquotesButton.php b/serendipity_event_typesetbuttons/buttons/SquotesButton.php new file mode 100644 index 00000000..2b29d850 --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/SquotesButton.php @@ -0,0 +1,137 @@ +setName('inssquote'); + } + + /** + * @return string + */ + public function render() + { + $this->overwriteValue(); + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + if ($this->isUseNamedEnts()) { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "']," . $this->getSurroundingNamedEntitiesStringByType() . ")"); + } else { + $this->setOnClickEvent("wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "']," . $this->getSurroundingStringByType() . ")"); + } + } else { + $this->addClass('wrap_selection'); + $namedEntities = $this->getCleanSurroundingStringByType(); + $tags = explode(',', $namedEntities); + $this->setOpenTag(trim($tags[0], '\'')); + $this->setCloseTag(trim($tags[1], '\'')); + } + return parent::render(); + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @param string $type + */ + public function setType($type) + { + $this->type = $type; + } + + /** + * Overwrite the value property according to given type + */ + private function overwriteValue() + { + $typeNumber = (int) preg_replace('$type$', '', $this->getType()); + $constName = 'PLUGIN_EVENT_TYPESETBUTTONS_SQUOTES' . $typeNumber . '_BUTTON'; + parent::setValue(constant($constName)); + } + + /** + * @return string + */ + private function getSurroundingStringByType() + { + $surroundingStrings = array( + 'type1' => "'\\&\\#8216\\;','\\&\\#8217\\;'", + 'type2' => "'\\&\\#8218\\;','\\&\\#8216\\;'", + 'type3' => "'\\&\\#8218\\;','\\&\\#8217\\;'", + 'type4' => "'\\&\\#8217\\;','\\&\\#8217\\;'", + 'type5' => "'\\&\\#8216\\;','\\&\\#8218\\;'", + 'type6' => "'\\&\\#8249\\;','\\&\\#8250\\;'", + 'type7' => "'\\&\\#8250\\;','\\&\\#8249\\;'", + 'type8' => "'\\&\\#8250\\;','\\&\\#8250\\;'", + ); + if (!array_key_exists($this->getType(), $surroundingStrings)) { + return $surroundingStrings['type1']; + } + return $surroundingStrings[$this->getType()]; + } + + /** + * @return string + */ + private function getSurroundingNamedEntitiesStringByType() + { + $surroundingStrings = array( + 'type1' => "'\\&lsquo\\;','\\&rsquo\\;'", + 'type2' => "'\\&sbquo\\;','\\&lsquo\\;'", + 'type3' => "'\\&sbquo\\;','\\&rsquo\\;'", + 'type4' => "'\\&rsquo\\;','\\&rsquo\\;'", + 'type5' => "'\\&lsquo\\;','\\&sbquo\\;'", + 'type6' => "'\\&lsaquo\\;','\\&rsaquo\\;'", + 'type7' => "'\\&rsaquo\\;','\\&lsaquo\\;'", + 'type8' => "'\\&rsaquo\\;','\\&rsaquo\\;'", + ); + if (!array_key_exists($this->getType(), $surroundingStrings)) { + return $surroundingStrings['type1']; + } + return $surroundingStrings[$this->getType()]; + } + + /** + * @return string + */ + private function getCleanSurroundingStringByType() + { + $surroundingStrings = array( + 'type1' => "'‘','’'", + 'type2' => "'‚','‘'", + 'type3' => "'‚','’'", + 'type4' => "'’','’'", + 'type5' => "'‘','‚'", + 'type6' => "'‹','›'", + 'type7' => "'›','‹'", + 'type8' => "'›','›'", + ); + if (!array_key_exists($this->getType(), $surroundingStrings)) { + return $surroundingStrings['type1']; + } + return $surroundingStrings[$this->getType()]; + } +} diff --git a/serendipity_event_typesetbuttons/buttons/StrikeButton.php b/serendipity_event_typesetbuttons/buttons/StrikeButton.php new file mode 100644 index 00000000..0c4fe3e1 --- /dev/null +++ b/serendipity_event_typesetbuttons/buttons/StrikeButton.php @@ -0,0 +1,46 @@ +setName('insstrike'); + $this->setValue(PLUGIN_EVENT_TYPESETBUTTONS_STRIKE_BUTTON); + } + + /** + * @return string + */ + public function render() + { + if ($this->isLegacyMode()) { + $this->addClass('serendipityPrettyButton'); + $this->addClass('input_button'); + if ($this->isXhtml11()) { + $this->setOnClickEvent( + "wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'','')" + ); + } else { + $this->setOnClickEvent( + "wrapSelection(document.forms['serendipityEntry']['" . $this->getTextarea() . "'],'','')" + ); + } + } else { + $this->addClass('wrap_selection'); + $this->addClass('lang-html'); + $this->setOpenTag('p style=\'text-decoration: line-through;\''); + $this->setCloseTag('p'); + } + return parent::render(); + } +} diff --git a/serendipity_event_typesetbuttons/serendipity_event_typesetbuttons.php b/serendipity_event_typesetbuttons/serendipity_event_typesetbuttons.php index f334fb66..f4c03811 100644 --- a/serendipity_event_typesetbuttons/serendipity_event_typesetbuttons.php +++ b/serendipity_event_typesetbuttons/serendipity_event_typesetbuttons.php @@ -1,37 +1,46 @@ add('name', PLUGIN_EVENT_TYPESETBUTTONS_TITLE); - $propbag->add('description', PLUGIN_EVENT_TYPESETBUTTONS_DESC); - $propbag->add('stackable', false); - $propbag->add('author', 'Matthew Groeninger, Malte Diers'); - $propbag->add('version', '0.11'); - $propbag->add('requirements', array( - 'serendipity' => '0.8', + $propbag->add('name', PLUGIN_EVENT_TYPESETBUTTONS_TITLE); + $propbag->add('description', PLUGIN_EVENT_TYPESETBUTTONS_DESC); + $propbag->add('stackable', false); + $propbag->add('author', 'Matthew Groeninger, Malte Diers, Matthias Gutjahr'); + $propbag->add('version', '0.22'); + $propbag->add('requirements', array( + 'serendipity' => '1.7', 'smarty' => '2.6.7', - 'php' => '4.1.0' + 'php' => '5.3.3' )); $propbag->add('configuration', array( 'enable_center', @@ -55,14 +64,19 @@ class serendipity_event_typesetbuttons extends serendipity_event 'use_named_ents', 'custom' )); - $propbag->add('event_hooks', array( + $propbag->add('event_hooks', array( 'backend_entry_toolbar_extended' => true, 'backend_entry_toolbar_body' => true )); $propbag->add('groups', array('BACKEND_EDITOR')); } - function introspect_config_item($name, &$propbag) + /** + * @param string $name + * @param serendipity_property_bag $propbag + * @return bool + */ + public function introspect_config_item($name, &$propbag) { switch ($name) { case 'custom': @@ -291,358 +305,167 @@ class serendipity_event_typesetbuttons extends serendipity_event return true; } - - function event_hook($event, &$bag, &$eventData, $addData = null) { + /** + * @param string $event + * @param serendipity_property_bag $bag + * @param array $eventData + * @return bool|true + */ + public function event_hook($event, &$bag, &$eventData) { global $serendipity; - + if (intval($serendipity['version'][0]) < 2) { + $this->legacy = true; + } $hooks = &$bag->get('event_hooks'); + $pluginConfigurationKeys = $bag->get('configuration'); if (isset($hooks[$event])) { - switch($event) { + switch ($event) { case 'backend_entry_toolbar_extended': - if (!$serendipity['wysiwyg']) { - if (isset($eventData['backend_entry_toolbar_extended:textarea'])) { - $txtarea = $eventData['backend_entry_toolbar_extended:textarea']; - } else { - $txtarea = "serendipity[extended]"; - } - $this->generate_button($txtarea); - return true; - } else { - return false; - } + return $this->processEvent('extended', $eventData, $pluginConfigurationKeys); break; - case 'backend_entry_toolbar_body': - if (!$serendipity['wysiwyg']) { - if (isset($eventData['backend_entry_toolbar_body:textarea'])) { - $txtarea = $eventData['backend_entry_toolbar_body:textarea']; - } else { - $txtarea = "serendipity[body]"; - } - $this->generate_button($txtarea); - return true; - } else { - return false; - } + return $this->processEvent('body', $eventData, $pluginConfigurationKeys); break; - default: return false; break; } - } else { - return false; } + return false; } - function generate_content(&$title) { + /** + * @param string $type + * @param array $eventData + * @param array $pluginConfigurationKeys + * @return bool + */ + private function processEvent($type, $eventData, $pluginConfigurationKeys) + { + global $serendipity; + if (!$serendipity['wysiwyg']) { + if (isset($eventData['backend_entry_toolbar_' . $type . ':textarea'])) { + $txtarea = $eventData['backend_entry_toolbar_' . $type . ':textarea']; + } else { + $txtarea = "serendipity[" . $type . "]"; + } + $this->generate_button($txtarea, $pluginConfigurationKeys); + return true; + } + return false; + } + /** + * @param string $title + * @return null|void + */ + public function generate_content(&$title) { $title = PLUGIN_EVENT_TYPESETBUTTONS_TITLE; } - function generate_button($txtarea) { - global $serendipity; - if (!isset($txtarea)) { - $txtarea = 'body'; + /** + * @param string $txtarea + * @param array $pluginConfigurationKeys + */ + private function generate_button($txtarea, array $pluginConfigurationKeys) { + global $serendipity; // required for optional logging of exceptions + if (!isset($txtarea)) { + $txtarea = 'body'; + } + $this->txtarea = $txtarea; + foreach ($pluginConfigurationKeys as $configKey) { + $keyParts = explode('_', $configKey); + if ($keyParts[0] !== 'enable' || $this->get_config($configKey) !== 'yes') { + continue; } - if ($this->get_config('enable_center') == 'yes') { - if ($this->get_config('use_xhtml11','yes') == 'yes') { -?> - -getButton($keyParts[1], $this->get_config('type_dquotes', 'type1')); + } elseif ($keyParts[1] === 'squotes') { + echo $html = $this->getButton($keyParts[1], $this->get_config('type_squotes', 'type1')); } else { -?> - -getButton($keyParts[1]); } + } catch (Exception $e) { + // Uncomment the next three lines for debugging: + // $fp = fopen($serendipity['serendipityPath'] . PATH_SMARTY_COMPILE . '/' . get_class($this) . '.log', 'a'); + // fwrite($fp, $e->getMessage() . PHP_EOL); + // fclose($fp); + continue; } - if ($this->get_config('enable_strike') == 'yes') { - if ($this->get_config('use_xhtml11','yes') == 'yes') { -?> - - - -get_config('custom'); + $custom = trim($custom); + if (!empty($custom)) { + echo '
'; + $parts = explode('|', $custom); + foreach ($parts as $part) { + $part = trim($part); + if (empty($part)) { + continue; } + echo $this->getCustomButton($txtarea, $part); } - if ($this->get_config('enable_space') == 'yes') { -?> - -get_config('enable_amp') == 'yes') { -?> - -get_config('enable_emdash') == 'yes') { - if ($this->get_config('use_named_ents') == 'yes') { -?> - - - -get_config('enable_endash') == 'yes') { - if ($this->get_config('use_named_ents') == 'yes') { -?> - - - -get_config('enable_bullet') == 'yes') { - if ($this->get_config('use_named_ents') == 'yes') { -?> - - - -get_config('enable_dquotes') == 'yes') { - switch($this->get_config('type_dquotes','type1')) { - case'type1': - if ($this->get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -txtarea); + $button->setIsLegacyMode($this->legacy); + if ($this->get_config('use_xhtml11') !== 'yes') { + $button->setIsXhtml11(false); + } + if ($this->get_config('use_named_ents') !== 'yes') { + $button->setUseNamedEnts(false); + } + if ($type !== null && method_exists($button, 'setType')) { + $button->setType($type); + } + if (method_exists($button, 'setUseRealApos')) { + if ($this->get_config('real_apos', 'yes') === 'no') { + $button->setUseRealApos(false); + } else { + $button->setUseRealApos(true); } - if ($this->get_config('enable_squotes') == 'yes') { - switch($this->get_config('type_squotes','type1')) { - case'type1': - if ($this->get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -get_config('use_named_ents') == 'yes') { -?> - - - -render(); + } - } - } - if ($this->get_config('enable_apos') == 'yes') { - if ($this->get_config('real_apos','yes') == 'no') { - if ($this->get_config('use_named_ents') == 'yes') { -?> - - - - - -get_config('enable_accent') == 'yes') { -?> - -get_config('enable_gaccent') == 'yes') { -?> - -get_config('custom'); - $custom = trim($custom); - if (!empty($custom)) { - echo '
'; - $parts = explode('|', $custom); - foreach($parts AS $idx => $part) { - $part = trim($part); - if (empty($part)) continue; - - $buttons = explode('@', $part); - $b_name = htmlspecialchars($buttons[0]); - $b_title = preg_replace('@[^a-z0-9]@i', '_', $buttons[0]); - $b_open = str_replace(array('"', "'"), array('"', "\\'"), $buttons[1]); - $b_close = str_replace(array('"', "'"), array('"', "\\'"), $buttons[2]); -?> - -setIsLegacyMode($this->legacy); + $button->setName('ins_custom_' . $b_name); + $button->setValue($b_title); + $button->setOpen($b_open); + $button->setClose($b_close); + return $button->render(); } } -/* vim: set sts=4 ts=4 expandtab : */ \ No newline at end of file +/* vim: set sts=4 ts=4 expandtab : */ diff --git a/serendipity_event_typesetbuttons/tests/buttons/AccentButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/AccentButtonTest.php new file mode 100644 index 00000000..0c0b832e --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/AccentButtonTest.php @@ -0,0 +1,40 @@ +button = new AccentButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/AmpButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/AmpButtonTest.php new file mode 100644 index 00000000..eae64bfa --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/AmpButtonTest.php @@ -0,0 +1,40 @@ +button = new AmpButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/AposButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/AposButtonTest.php new file mode 100644 index 00000000..7985fb52 --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/AposButtonTest.php @@ -0,0 +1,63 @@ +button = new AposButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyModeNoRealApos() + { + $this->button->setIsLegacyMode(true); + $this->button->setUseRealApos(false); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyModeNoRealAposAndNoNamedEnts() + { + $this->button->setIsLegacyMode(true); + $this->button->setUseRealApos(false); + $this->button->setUseNamedEnts(false); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/BulletButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/BulletButtonTest.php new file mode 100644 index 00000000..db0640ff --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/BulletButtonTest.php @@ -0,0 +1,52 @@ +button = new BulletButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyModeNoNamedEnts() + { + $this->button->setIsLegacyMode(true); + $this->button->setUseNamedEnts(false); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/CenterButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/CenterButtonTest.php new file mode 100644 index 00000000..5a46adbb --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/CenterButtonTest.php @@ -0,0 +1,51 @@ +button = new CenterButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $this->button->setIsXhtml11(false); + $expected = " ','')\" />" . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyModeAndXhtml11() + { + $this->button->setIsLegacyMode(true); + $expected = " ','')\" />" . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/CustomButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/CustomButtonTest.php new file mode 100644 index 00000000..f04b7253 --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/CustomButtonTest.php @@ -0,0 +1,67 @@ +button = new CustomButton('serendipity[body]'); + } + + /** + * @test + * @dataProvider getCustomData + */ + public function render($name, $openTag, $closeTag) + { + $this->button->setName('ins_custom_' . $name); + $this->button->setValue($name); + $this->button->setOpen($openTag); + $this->button->setClose($closeTag); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + * @dataProvider getCustomData + */ + public function renderInLegacyMode($name, $openTag, $closeTag) + { + $this->button->setIsLegacyMode(true); + $this->button->setName('ins_custom_' . $name); + $this->button->setValue($name); + $this->button->setOpen($openTag); + $this->button->setClose($closeTag); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * Data provider for custom buttons + * + * @return array + */ + public function getCustomData() + { + return array( + array('code', '', ''), + array('pre', '
', '
'), + array('bash', '[geshi lang=bash]', '[/geshi]'), + array('perl', '[geshi lang=perl]', '[/geshi]'), + array('sql', '[geshi lang=sql]', '[/geshi]'), + array('li', '
  • ', '
  • '), + ); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/DquotesButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/DquotesButtonTest.php new file mode 100644 index 00000000..a8e42887 --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/DquotesButtonTest.php @@ -0,0 +1,81 @@ +button = new DquotesButton('serendipity[body]'); + } + + /** + * @test + * @dataProvider getNamedEntities + */ + public function render($type, $openTag, $closeTag, $namedEntity) + { + $this->button->setType($type); + $html = sprintf( + '', + $openTag, + $closeTag, + $namedEntity + ); + $expected = ' ' . $html . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * Data provider for named entities + * + * @return array + */ + public function getNamedEntities() + { + return array( + array('type1', '“', '”', '“ ”'), + array('type2', '„', '“', '„ “'), + array('type3', '„', '”', '„ ”'), + array('type4', '”', '”', '” ”'), + array('type5', '“', '„', '“ „'), + array('type6', '« ', ' »', '«   »'), + array('type7', '»', '«', '» «'), + array('type8', '»', '»', '» »'), + ); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $this->button->setType('type1'); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyModeNoNamedEnts() + { + $this->button->setIsLegacyMode(true); + $this->button->setUseNamedEnts(false); + $this->button->setType('type1'); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/EmdashButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/EmdashButtonTest.php new file mode 100644 index 00000000..cf2bba0d --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/EmdashButtonTest.php @@ -0,0 +1,57 @@ +button = new EmdashButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $html = sprintf( + '', + '—', + '', + '—' + ); + $expected = ' ' . $html . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyModeNoNamedEnts() + { + $this->button->setIsLegacyMode(true); + $this->button->setUseNamedEnts(false); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/EndashButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/EndashButtonTest.php new file mode 100644 index 00000000..2ee7c5f5 --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/EndashButtonTest.php @@ -0,0 +1,57 @@ +button = new EndashButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $html = sprintf( + '', + '–', + '', + '–' + ); + $expected = ' ' . $html . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyModeNoNamedEnts() + { + $this->button->setIsLegacyMode(true); + $this->button->setUseNamedEnts(false); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/GaccentButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/GaccentButtonTest.php new file mode 100644 index 00000000..c83925e9 --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/GaccentButtonTest.php @@ -0,0 +1,40 @@ +button = new GaccentButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/SpaceButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/SpaceButtonTest.php new file mode 100644 index 00000000..2a9e06dd --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/SpaceButtonTest.php @@ -0,0 +1,40 @@ +button = new SpaceButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/SquotesButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/SquotesButtonTest.php new file mode 100644 index 00000000..aa01d69e --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/SquotesButtonTest.php @@ -0,0 +1,80 @@ +button = new SquotesButton('serendipity[body]'); + } + + /** + * @test + * @dataProvider getNamedEntities + */ + public function render($type, $openTag, $closeTag, $namedEntity) + { + $this->button->setType($type); + $html = sprintf( + '', + $openTag, + $closeTag, + $namedEntity + ); + $expected = ' ' . $html . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * Data provider for named entities + * + * @return array + */ + public function getNamedEntities() + { + return array( + array('type1', '‘', '’', '‘ ’'), + array('type2', '‚', '‘', '‚ ‘'), + array('type3', '‚', '’', '‚ ’'), + array('type4', '’', '’', '’ ’'), + array('type5', '‘', '‚', '‘ ‚'), + array('type6', '‹', '›', '‹ ›'), + array('type7', '›', '‹', '› ‹'), + array('type8', '›', '›', '› ›'), + ); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $this->button->setType('type1'); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyModeNoNamedEnts() + { + $this->button->setIsLegacyMode(true); + $this->button->setUseNamedEnts(false); + $this->button->setType('type1'); + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/buttons/StrikeButtonTest.php b/serendipity_event_typesetbuttons/tests/buttons/StrikeButtonTest.php new file mode 100644 index 00000000..d58948e9 --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/buttons/StrikeButtonTest.php @@ -0,0 +1,51 @@ +button = new StrikeButton('serendipity[body]'); + } + + /** + * @test + */ + public function render() + { + $expected = ' ' . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyMode() + { + $this->button->setIsLegacyMode(true); + $this->button->setIsXhtml11(false); + $expected = " ','')\" />" . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } + + /** + * @test + */ + public function renderInLegacyModeAndXhtml11() + { + $this->button->setIsLegacyMode(true); + $expected = " ','')\" />" . PHP_EOL; + $this->assertEquals($expected, $this->button->render()); + } +} diff --git a/serendipity_event_typesetbuttons/tests/typesetbuttonsTest.php b/serendipity_event_typesetbuttons/tests/typesetbuttonsTest.php new file mode 100644 index 00000000..43f88931 --- /dev/null +++ b/serendipity_event_typesetbuttons/tests/typesetbuttonsTest.php @@ -0,0 +1,349 @@ +object = new serendipity_event_typesetbuttons('test'); + $this->propBag = new serendipity_property_bag(); + $this->getEventData(); + } + + /** + * Tear down + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * Helper method + */ + protected function getEventData() + { + $this->eventData = array( + 'id' => 1, + 'body' => 'Normal body.', + 'extended' => 'Extended body.', + ); + } + + /** + * Example 1 + * @test + */ + public function introspect() + { + $this->object->introspect($this->propBag); + $this->assertEquals('Typeset/Extended Buttons for non-WYSIWYG editors', $this->propBag->get('name')); + $this->assertFalse($this->propBag->get('stackable')); + } + + /** + * @test + */ + public function generateContent() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $title = 'foobar'; // we need to pass this by reference + $this->object->generate_content($title); + $this->assertEquals('Typeset/Extended Buttons for non-WYSIWYG editors', $title); + } + + /** + * @test + */ + public function wrongEventShouldReturnFalse() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $this->object->introspect($this->propBag); + $this->assertFalse($this->object->event_hook('frontend_comment', $this->propBag, $this->eventData)); + } + + /** + * @test + */ + public function extendedToolbarShouldNotBeAffectedIfWysiwygIsActive() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $serendipity['wysiwyg'] = true; // WYSIWYG editor is active + $this->object->introspect($this->propBag); + $result = $this->object->event_hook('backend_entry_toolbar_extended', $this->propBag, $this->eventData); + $this->assertFalse($result); + } + + /** + * @test + */ + public function extendedToolbarWithExtendedTextareaShouldOutputButtons() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $serendipity['wysiwyg'] = false; // WYSIWYG editor is inactive + $this->object->introspect($this->propBag); + $this->eventData['backend_entry_toolbar_extended:textarea'] = 'serendipity[extended]'; + $result = $this->object->event_hook('backend_entry_toolbar_extended', $this->propBag, $this->eventData); + $this->expectOutputString($this->getNotXhtml11Output('serendipity[extended]')); + $this->assertTrue($result); + } + + /** + * @test + */ + public function extendedToolbarWithExtendedTextareaShouldOutputCustomButtons() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $serendipity['wysiwyg'] = false; // WYSIWYG editor is inactive + $this->object->introspect($this->propBag); + $customButtons = <<@| +pre@
    @
    | +bash@[geshi lang=bash]@[/geshi]| +perl@[geshi lang=perl]@[/geshi]| +sql@[geshi lang=sql]@[/geshi]| +li@
  • @
  • +EOT; + $this->object->set_config('custom', $customButtons); + $this->eventData['backend_entry_toolbar_extended:textarea'] = 'serendipity[extended]'; + $result = $this->object->event_hook('backend_entry_toolbar_extended', $this->propBag, $this->eventData); + $this->expectOutputString($this->getNotXhtml11OutputWithCustomButtons('serendipity[extended]')); + $this->assertTrue($result); + } + + /** + * @test + */ + public function extendedToolbarWithExtendedTextareaShouldOutputXhtml11Buttons() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $serendipity['wysiwyg'] = false; // WYSIWYG editor is inactive + $this->object->introspect($this->propBag); + $this->object->set_config('use_xhtml11', 'no'); + $this->eventData['backend_entry_toolbar_extended:textarea'] = 'serendipity[extended]'; + $result = $this->object->event_hook('backend_entry_toolbar_extended', $this->propBag, $this->eventData); + $this->expectOutputString($this->getXhtml11Output('serendipity[extended]')); + $this->assertTrue($result); + } + + /** + * @test + */ + public function extendedToolbarWithCustomTextareaShouldOutputButtons() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $serendipity['wysiwyg'] = false; // WYSIWYG editor is inactive + $this->object->introspect($this->propBag); + $this->eventData['backend_entry_toolbar_extended:textarea'] = 'foobar'; + $result = $this->object->event_hook('backend_entry_toolbar_extended', $this->propBag, $this->eventData); + $this->expectOutputString($this->getNotXhtml11Output('foobar')); + $this->assertTrue($result); + } + + /** + * @test + */ + public function extendedToolbarWithBodyTextareaShouldOutputButtons() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $serendipity['wysiwyg'] = false; // WYSIWYG editor is inactive + $this->object->introspect($this->propBag); + $this->eventData['backend_entry_toolbar_body:textarea'] = 'serendipity[body]'; + $result = $this->object->event_hook('backend_entry_toolbar_body', $this->propBag, $this->eventData); + $this->expectOutputString($this->getNotXhtml11Output('serendipity[body]')); + $this->assertTrue($result); + } + + /** + * @test + */ + public function disabledButtonsShouldNotBeDisplayed() + { + global $serendipity; + $serendipity['version'] = '2.0.0'; + $serendipity['wysiwyg'] = false; // WYSIWYG editor is inactive + $this->object->introspect($this->propBag); + $this->object->set_config('enable_center', 'no'); + $this->object->set_config('enable_strike', 'no'); + $this->eventData['backend_entry_toolbar_body:textarea'] = 'serendipity[body]'; + $result = $this->object->event_hook('backend_entry_toolbar_body', $this->propBag, $this->eventData); + $this->expectOutputString($this->getNotXhtml11OutputWithoutDisabledButtons('serendipity[body]')); + $this->assertTrue($result); + } + + /** + * @test + */ + public function disabledButtonsShouldNotBeDisplayedInLegacyMode() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $serendipity['wysiwyg'] = false; // WYSIWYG editor is inactive + $this->object->introspect($this->propBag); + $this->object->set_config('enable_center', 'no'); + $this->object->set_config('enable_strike', 'no'); + $this->eventData['backend_entry_toolbar_body:textarea'] = 'serendipity[body]'; + $result = $this->object->event_hook('backend_entry_toolbar_body', $this->propBag, $this->eventData); + $this->expectOutputString($this->getNotXhtml11OutputWithoutDisabledButtonsInLegacyMode('serendipity[body]')); + $this->assertTrue($result); + } + + /** + * @test + */ + public function extendedToolbarWithCustomBodyTextareaShouldOutputButtons() + { + global $serendipity; + $serendipity['version'] = '1.7.8'; + $serendipity['wysiwyg'] = false; // WYSIWYG editor is inactive + $this->object->introspect($this->propBag); + $this->eventData['backend_entry_toolbar_body:textarea'] = 'foobar'; + $result = $this->object->event_hook('backend_entry_toolbar_body', $this->propBag, $this->eventData); + $this->expectOutputString($this->getNotXhtml11Output('foobar')); + $this->assertTrue($result); + } + + /** + * @param string $textarea + * @return string + */ + private function getXhtml11Output($textarea = "serendipity[extended]") + { + $output = " ','')\" /> + ','')\" /> + + + + + + + + + + +"; + return $output; + } + + /** + * @param string $textarea + * @return string + */ + private function getNotXhtml11Output($textarea = "serendipity[extended]") + { + $output = " ','')\" /> + ','')\" /> + + + + + + + + + + +"; + return $output; + } + + /** + * @param string $textarea + * @return string + */ + private function getNotXhtml11OutputWithoutDisabledButtons($textarea = "serendipity[extended]") + { + $output = " + + + + + + + + + +"; + return $output; + } + + /** + * @param string $textarea + * @return string + */ + private function getNotXhtml11OutputWithoutDisabledButtonsInLegacyMode($textarea = "serendipity[extended]") + { + $output = " + + + + + + + + + +"; + return $output; + } + + /** + * @param string $textarea + * @return string + */ + private function getNotXhtml11OutputWithCustomButtons($textarea = "serendipity[extended]") + { + $output = " ','')\" /> + ','')\" /> + + + + + + + + + + +
    ', '')\" /> + ', '')\" /> + + + + ', '')\" /> +"; + return $output; + } +}