<?php

/**
 * Test class to test rcmail_action_mail_compose
 *
 * @package Tests
 */
class Actions_Mail_Compose extends ActionTestCase
{
    /**
     * Class constructor
     */
    function test_class()
    {
        $object = new rcmail_action_mail_compose;

        $this->assertInstanceOf('rcmail_action', $object);
    }

    /**
     * Test quote_text() method
     */
    function test_quote_text()
    {
        $action = new rcmail_action_mail_compose;

        $this->assertSame('> ', $action->quote_text(''));

        $result = $action->quote_text("test1\ntest2");
        $expected = "> test1\n> test2";

        $this->assertSame($expected, $result);

        $result = $action->quote_text("> test1\n> test2");
        $expected = ">> test1\n>> test2";

        $this->assertSame($expected, $result);
    }

    /**
     * Test compose_part_body() handling of text/enriched content
     */
    public function test_compose_part_body_enriched()
    {
        $message = new MessageMock(123);
        $set_part = function ($body) use ($message) {
            $part = new rcube_message_part();
            $part->mime_id = '1';
            [$part->ctype_primary, $part->ctype_secondary] = explode('/', $part->mimetype = 'text/enriched');
            $message->set_part_body(1, $body);
            return $part;
        };

        $action = new rcmail_action_mail_compose();
        setProperty($action, 'MESSAGE', $message);
        setProperty($action, 'COMPOSE', ['mode' => rcmail_sendmail::MODE_NONE]);

        $part = $set_part('<italic>the-text</italic>Test<script>alert(1)</script>');
        $result = $action->compose_part_body($part, true);
        $this->assertSame('<div id="nonebody1"><i>the-text</i>Test</div>', $result);

        $part = $set_part('<italic>the-text</italic>Test<script>alert(1)</script>');
        $result = $action->compose_part_body($part, false);
        $this->assertSame('_the-text_Test', $result);
    }
}
