MODE=TAP,9

BEGINPERL

my $mech;

subtest 'Login' => sub {
    plan tests => 4;

    $mech = webui_login(
        username => 'user3',
        password => 'user3',
        url      => "http://$webhost",
    );
};

$mech->follow_link( text_regex => qr/View My Details/ );
$mech->text_contains('Principal: User 3', 'Correct users edit page');

# Create bind into another users namespace
$mech->submit_form_ok(
    {
        form_number => 4,
        button      => 'bindingrow',
        fields      => {
            dav_name        => '/user1/bogus',
            dav_displayname => 'Bogus bind',
            source          => 'http://regression/testfiles/bogus.ics',
        },
    }, "Create bind into another users namespace - should fail"
);

$mech->content_contains(
    'Can only bind collections into the current principal\'s namespace',
    'Error present');

# Create bind
$mech->submit_form_ok(
    {
        form_number => 4,
        button      => 'bindingrow',
        fields      => {
            dav_name        => '/user3/moon',
            dav_displayname => 'Moon Phases - user3',
            source          => 'http://regression/testfiles/mooncal.ics?lang=de&phases%5Bfull%5D=true&phases%5Bnew%5D=true&phases%5Bquarter%5D=true&phases%5Bdaily%5D=false&events%5Blunareclipse%5D=true&events%5Bsolareclipse%5D=true&events%5Bmoonlanding%5D=false&before=P6M&after=P2Y&zone=CET',
        },
    }, "Submit updated fields"
);

$mech->save_content('/tmp/form.html');

$mech->content_contains(
    'Creating new binding for this principal',
    'Correct status message is present');

$mech->content_contains(
    'Moon Phases - user3',
    'Binding is present');

# Create duplicate bind - should fail
$mech->submit_form_ok(
    {
        form_number => 4,
        button      => 'bindingrow',
        fields      => {
            dav_name        => '/user3/moon',
            dav_displayname => 'Moon Phases - user3',
            source          => 'http://regression/testfiles/bogus.ics',
        },
    }, "Submit updated fields"
);

$mech->content_contains(
    'A resource already exists at the destination.',
    'Correct error message is present');

ENDPERL

# Bogus bind isn't present - should be 0.
BEGINQUERY
SELECT count(*)
FROM dav_binding
WHERE dav_displayname = 'Bogus Bind'
ENDQUERY

# Bind is present with correct URL
BEGINQUERY
SELECT bind_id,
       bound_source_id,
       parent_container,
       dav_name,
       dav_displayname,
       external_url
FROM dav_binding
WHERE dav_displayname = 'Moon Phases - user3'
ORDER BY bind_id
ENDQUERY
