#!perl
use Cassandane::Tiny;

sub test_control_chars_repaired
    :min_version_3_0 :NoStartInstances
{
    my ($self) = @_;

    # from 3.0-3.2, this behaviour was optional and required the
    # carddav_repair_vcard switch to be set
    my ($maj, $min) = Cassandane::Instance->get_version();
    if ($maj == 3 && ($min >= 0 && $min <= 2)) {
        $self->{instance}->{config}->set('carddav_repair_vcard' => 'yes');
    }
    $self->_start_instances();

    # :NoStartInstances magic means set_up() didn't do this bit for us
    my $service = $self->{instance}->get_service("http");
    $ENV{DEBUGDAV} = 1;
    $self->{carddav} = Net::CardDAVTalk->new(
        user => 'cassandane',
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    my $CardDAV = $self->{carddav};
    my $Id = $CardDAV->NewAddressBook('foo');
    $self->assert_not_null($Id);
    $self->assert_str_equals($Id, 'foo');
    my $href = "$Id/bar.vcf";

    my $card = <<EOF;
BEGIN:VCARD
VERSION:3.0
UID:123456789
N:Gump;Forrest;;Mr.
FN:Forrest\b Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
REV:2008-04-24T19:52:43Z
END:VCARD
EOF

    # the \b should be repaired out
    my $VCard = Net::CardDAVTalk::VCard->new_fromstring($card);
    my $path = $CardDAV->NewContact($Id, $VCard);
    my $res = $CardDAV->GetContact($path);
    $self->assert_str_equals($res->{properties}{fn}[0]{value}, 'Forrest Gump');
}
