#!perl
use Cassandane::Tiny;

sub test_addressbook_query_match_endswith ($self)
{
    my $carddav = $self->{carddav};
    my $bookId  = "Default";
    my $homeset = "/dav/addressbooks/user/cassandane";

    my $uid1 = guid_string();
    my $uid2 = guid_string();

    my $vcard1 = vcard_to_jscontact(<<~"EOF");
        BEGIN:VCARD
        VERSION:3.0
        UID:$uid1
        FN:Alice Wonderland
        N:Wonderland;Alice;;;
        END:VCARD
        EOF

    my $vcard2 = vcard_to_jscontact(<<~"EOF");
        BEGIN:VCARD
        VERSION:3.0
        UID:$uid2
        FN:Bob Smith
        N:Smith;Bob;;;
        END:VCARD
        EOF

    my $href1 = $carddav->NewContact($bookId, $vcard1);
    my $href2 = $carddav->NewContact($bookId, $vcard2);

    my $xml = <<~"EOF";
        <C:addressbook-query xmlns:D="DAV:"
                             xmlns:C="urn:ietf:params:xml:ns:carddav">
          <D:prop><D:getetag/></D:prop>
          <C:filter>
            <C:prop-filter name="FN">
              <C:text-match collation="i;unicode-casemap"
                            match-type="ends-with">land</C:text-match>
            </C:prop-filter>
          </C:filter>
        </C:addressbook-query>
        EOF

    my $res = $carddav->Request('REPORT', "$homeset/$bookId",
                                $xml, Depth => 0, 'Content-Type' => 'text/xml');

    $self->assert_num_equals(1, scalar $res->{"{DAV:}response"}->@*);
    $self->assert_str_equals("$homeset/$href1",
                             $res->{"{DAV:}response"}[0]{"{DAV:}href"}{content});
}
