RT-Extension-REST2

 view release on metacpan or  search on metacpan

xt/ticket-customfields.t  view on Meta::CPAN

        cmp_deeply($ticket->{'CustomFields'}, $ticket_cf_value, 'Ticket custom field');
    }
}

{
    sub modify_multi_ok {
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my $input = shift;
        my $messages = shift;
        my $output = shift;
        my $name = shift;

        my $payload = {
            CustomFields => {
                $multi_cf_id => $input,
            },
        };
        my $res = $mech->put_json($ticket_url,
            $payload,
            'Authorization' => $auth,
        );
        is($res->code, 200);
        is_deeply($mech->json_response, $messages);

        $res = $mech->get($ticket_url,
            'Authorization' => $auth,
        );
        is($res->code, 200);

        my $ticket_cf_value = bag(
            { name => 'Single', id => $single_cf_id, type => 'customfield', _url => ignore(), values => [] },
            { name => 'Multi',  id => $multi_cf_id,  type => 'customfield', _url => ignore(), values => bag(@$output) },
        );

        my $content = $mech->json_response;
        cmp_deeply($content->{'CustomFields'}, $ticket_cf_value, $name || 'New CF value');
    }

    # starting point: ['multiple', 'values'],
    modify_multi_ok(['multiple', 'values'], [], ['multiple', 'values'], 'no change');
    modify_multi_ok(['multiple', 'values', 'new'], ['new added as a value for Multi'], ['multiple', 'new', 'values'], 'added "new"');
    modify_multi_ok(['multiple', 'new'], ['values is no longer a value for custom field Multi'], ['multiple', 'new'], 'removed "values"');
    modify_multi_ok('replace all', ['replace all added as a value for Multi', 'multiple is no longer a value for custom field Multi', 'new is no longer a value for custom field Multi'], ['replace all'], 'replaced all values');
    modify_multi_ok([], ['replace all is no longer a value for custom field Multi'], [], 'removed all values');

    if (RT::Handle::cmp_version($RT::VERSION, '4.2.5') >= 0) {
        modify_multi_ok(['foo', 'foo', 'bar'], ['foo added as a value for Multi', undef, 'bar added as a value for Multi'], ['bar', 'foo'], 'multiple values with the same name');
        modify_multi_ok(['foo', 'bar'], [], ['bar', 'foo'], 'multiple values with the same name');
        modify_multi_ok(['bar'], ['foo is no longer a value for custom field Multi'], ['bar'], 'multiple values with the same name');
        modify_multi_ok(['bar', 'bar', 'bar'], [undef, undef], ['bar'], 'multiple values with the same name');
    } else {
        modify_multi_ok(['foo', 'foo', 'bar'], ['foo added as a value for Multi', 'foo added as a value for Multi', 'bar added as a value for Multi'], ['bar', 'foo', 'foo'], 'multiple values with the same name');
        modify_multi_ok(['foo', 'bar'], ['foo is no longer a value for custom field Multi'], ['bar', 'foo'], 'multiple values with the same name');
        modify_multi_ok(['bar'], ['foo is no longer a value for custom field Multi'], ['bar'], 'multiple values with the same name');
        modify_multi_ok(['bar', 'bar', 'bar'], ['bar added as a value for Multi', 'bar added as a value for Multi'], ['bar', 'bar', 'bar'], 'multiple values with the same name');
    }
}

# Ticket Creation with image CF through JSON Base64
my $image_name = 'image.png';
my $image_path = RT::Test::get_relocatable_file($image_name, 'data');
my $image_content;
open my $fh, '<', $image_path or die "Cannot read $image_path: $!\n";
{
    local $/;
    $image_content = <$fh>;
}
close $fh;
my $image_cf = RT::CustomField->new(RT->SystemUser);
$image_cf->Create(LookupType => 'RT::Queue-RT::Ticket', Name => 'Image CF', Type => 'Image', MaxValues => 1, Queue => 'General');
my $image_cf_id = $image_cf->id;
{
    my $payload = {
        Subject => 'Ticket creation with image CF',
        From    => 'test@bestpractical.com',
        To      => 'rt@localhost',
        Queue   => 'General',
        Content => 'Testing ticket creation with Base64 encoded Image Custom Field using REST API.',
        CustomFields => {
            $image_cf_id => {
                FileName => $image_name,
                FileType => 'image/png',
                FileContent => MIME::Base64::encode_base64($image_content),
            },
        },
    };

    my $res = $mech->post_json("$rest_base_path/ticket",
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 201);
    ok($ticket_url = $res->header('location'));
    ok(($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)]);

    my $ticket = RT::Ticket->new($user);
    $ticket->Load($ticket_id);
    my $image_ocfv = $ticket->CustomFieldValues('Image CF')->First;
    is($image_ocfv->Content, $image_name);
    is($image_ocfv->ContentType, 'image/png');
    is($image_ocfv->LargeContent, $image_content);
}

# Ticket Update with image CF through JSON Base64
{
    # Ticket update to delete image CF
    my $payload = {
        CustomFields => {
            $image_cf_id => undef,
        },
    };

    my $res = $mech->put_json($ticket_url,
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 200);

    my $ticket = RT::Ticket->new($user);
    $ticket->Load($ticket_id);
    my $image_ocfv = $ticket->CustomFieldValues('Image CF')->First;



( run in 0.592 second using v1.01-cache-2.11-cpan-5511b514fd6 )