[Rt-commit] rt branch, 5.0/remove-catalog-modal, created. rt-4.4.4-768-gcad2a52c8

Michel Rodriguez michel at bestpractical.com
Wed Feb 26 10:33:49 EST 2020


The branch, 5.0/remove-catalog-modal has been created
        at  cad2a52c8eb30a393826c30a381cde8c8b26e473 (commit)

- Log -----------------------------------------------------------------
commit 04a53ab6513bbc832523df9010c309cc5bddc64b
Author: michel <michel at bestpractical.com>
Date:   Thu Feb 20 15:30:55 2020 +0100

    Select Catalog in Asset Create page.
    
    Allow Catalog to be selected in the create asset page, using defaults
    from configuration if none provided. Existing urls for asset creation
    will still work.

diff --git a/share/html/Admin/Assets/Catalogs/Modify.html b/share/html/Admin/Assets/Catalogs/Modify.html
index e27bc2fd3..2b82f50d3 100644
--- a/share/html/Admin/Assets/Catalogs/Modify.html
+++ b/share/html/Admin/Assets/Catalogs/Modify.html
@@ -51,6 +51,7 @@
 
 <form method="post" enctype="multipart/form-data" id="ModifyCatalog" action="Modify.html">
   <input type="hidden" name="id" value="<% $catalog->id %>">
+  <input type="hidden" name="CatalogChanged" value="0">
 
   <&| /Widgets/TitleBox, title => loc("Basics"), class => "catalog-basics" &>
     <& Elements/EditBasics, %ARGS, CatalogObj => $catalog &>
diff --git a/share/html/Asset/Create.html b/share/html/Asset/Create.html
index 12a3bd1f2..d79997906 100644
--- a/share/html/Asset/Create.html
+++ b/share/html/Asset/Create.html
@@ -51,16 +51,16 @@
 <& /Elements/ListActions, actions => \@results &>
 
 <span class="catalog <% CSSClass($catalog->Name) %>">
-  <form method="post" enctype="multipart/form-data" id="CreateAsset" action="Create.html">
+  <form method="post" enctype="multipart/form-data" id="CreateAsset" name="CreateAsset" action="Create.html">
     <input type="hidden" name="id" value="new">
-    <input type="hidden" name="Catalog" value="<% $catalog->id %>">
+    <input type="hidden" name="CatalogChanged" value="0" />
 
     <div id="Asset-Create-basics" class="asset-metadata form-row">
       <a name="basics"></a>
       <div class="col-md-6">
         <&| /Widgets/TitleBox, title => loc("Basics"), class => "asset-basics", title_class => "inverse" &>
-          <& Elements/EditBasics, %ARGS, AssetObj => $asset, CatalogObj => $catalog &>
-        </&>
+          <& Elements/EditBasics, %ARGS, AssetObj => $asset, CatalogObj => $catalog, AutoSubmit => 1&>
+       </&>
       </div>
 
       <div class="col-md-6">
@@ -108,7 +108,12 @@ $Catalog => undef
 <%init>
 my $asset   = RT::Asset->new( $session{CurrentUser} );
 my $catalog = RT::Catalog->new( $session{CurrentUser} );
-$catalog->Load($Catalog);
+if ( $Catalog ) {
+    $catalog->Load($Catalog);
+}
+else {
+    $catalog = LoadDefaultCatalog( $ARGS{Catalog} || '' );
+}
 
 Abort(loc("Unable to find catalog '[_1]'", $Catalog))
     unless $catalog->id;
@@ -132,6 +137,10 @@ if ($id eq "new") {
         ARGSRef         => \%ARGS
     );
 
+    if ( $ARGS{CatalogChanged} ) {
+        $skip_create = 1;
+    }
+
     $m->callback(
         CallbackName    => 'BeforeCreate',
         AssetObj        => $asset,
diff --git a/share/html/Asset/Elements/EditBasics b/share/html/Asset/Elements/EditBasics
index 798d98261..e4ba222b5 100644
--- a/share/html/Asset/Elements/EditBasics
+++ b/share/html/Asset/Elements/EditBasics
@@ -45,16 +45,14 @@
 %# those contributions and any derivatives thereof.
 %#
 %# END BPS TAGGED BLOCK }}}
-% if ($AssetObj->id) {
   <div class="form-row asset-catalog">
     <div class="label col-md-3">
       <&|/l&>Catalog</&>:
     </div>
     <div class="value col-md-9">
-      <& /Asset/Elements/SelectCatalog, Default => $current{Catalog} &>
+      <& /Asset/Elements/SelectCatalog, Default => $current{Catalog}, AutoSubmit => $AutoSubmit &>
     </div>
   </div>
-% }
   <div class="form-row asset-name">
     <div class="label col-md-3">
       <&|/l&>Name</&>:
@@ -95,8 +93,10 @@
 <%args>
 $AssetObj
 $CatalogObj => undef
+$AutoSubmit => 0
 </%args>
 <%init>
 my %current = map { $_ => ($ARGS{$_} || $AssetObj->$_ || '') }
                   $AssetObj->WritableAttributes;
+$CatalogObj ||= LoadDefaultCatalog($ARGS{'Catalog'} || ($AssetObj && $AssetObj->Catalog) || '' );
 </%init>
diff --git a/share/html/Asset/Elements/SelectCatalog b/share/html/Asset/Elements/SelectCatalog
index 76a73de21..0c52fff68 100644
--- a/share/html/Asset/Elements/SelectCatalog
+++ b/share/html/Asset/Elements/SelectCatalog
@@ -60,6 +60,7 @@ $ShowAll => 0
 $Default => undef
 $UpdateSession => 1
 $ShowNullOption => 0
+$AutoSubmit      => 0
 </%args>
 <%init>
 my $catalog_obj = LoadDefaultCatalog($Default || '');
@@ -67,4 +68,6 @@ if ( $UpdateSession && $catalog_obj->Id ){
     $session{'DefaultCatalog'} = $catalog_obj->Id;
     $Default = $catalog_obj->Id;
 }
+$ARGS{OnChange} = "jQuery(this).closest('form').find('input[name=CatalogChanged]').val(1);";
+$ARGS{OnChange} .= "jQuery(this).closest('form').submit();" if $AutoSubmit;
 </%init>

commit cad2a52c8eb30a393826c30a381cde8c8b26e473
Author: michel <michel at bestpractical.com>
Date:   Wed Feb 26 15:37:12 2020 +0100

    Test catalog select on asset create page

diff --git a/lib/RT/Test/Assets.pm b/lib/RT/Test/Assets.pm
index 57dcbb524..193d15f64 100644
--- a/lib/RT/Test/Assets.pm
+++ b/lib/RT/Test/Assets.pm
@@ -80,6 +80,24 @@ sub create_catalog {
     }
 }
 
+sub load_or_create_catalog {
+    my $self = shift;
+    my %args = ( Disabled => 0, @_ );
+    my $obj = RT::Catalog->new( RT->SystemUser );
+    if ( $args{'Name'} ) {
+        $obj->LoadByCols( Name => $args{'Name'} );
+    } else {
+        die "Name is required";
+    }
+    unless ( $obj->id ) {
+        my ($val, $msg) = $obj->Create( %args );
+        die "$msg" unless $val;
+    }
+
+    return $obj;
+}
+
+
 sub create_asset {
     my %info  = @_;
     my $asset = RT::Asset->new( RT->SystemUser );
@@ -128,4 +146,15 @@ sub apply_cfs {
     return $success;
 }
 
+sub last_asset {
+    my $self = shift;
+    my $current = shift;
+    $current = $current ? RT::CurrentUser->new($current) : RT->SystemUser;
+    my $assets = RT::Assets->new( $current );
+    $assets->OrderBy( FIELD => 'id', ORDER => 'DESC' );
+    $assets->Limit( FIELD => 'id', OPERATOR => '>', VALUE => '0' );
+    $assets->RowsPerPage( 1 );
+    return $assets->First;
+}
+
 1;
diff --git a/t/web/asset_create.t b/t/web/asset_create.t
new file mode 100644
index 000000000..ad9ab36ba
--- /dev/null
+++ b/t/web/asset_create.t
@@ -0,0 +1,85 @@
+use strict;
+use warnings;
+
+use RT::Test::Assets tests => undef;
+use RT::Lifecycle;
+
+# populate lifecycles
+my $lifecycles = RT->Config->Get('Lifecycles');
+RT->Config->Set(
+    Lifecycles => %{$lifecycles},
+    foo        => {
+        initial  => ['initial'],
+        active   => ['tracked'],
+        inactive => ['retired'],
+    }
+);
+RT::Lifecycle->FillCache();
+
+RT->Config->Set( DefaultCatalog => 'Default catalog' );
+
+# populate test catalogs and test user
+my $catalog_1 = RT::Test::Assets->load_or_create_catalog( Name => 'Default catalog' );
+ok( $catalog_1, 'created catalog 1' );
+my $catalog_2 = RT::Test::Assets::->load_or_create_catalog( Name => 'Another catalog' );
+ok( $catalog_2, 'created catalog 2' );
+my $user   = RT::Test::Assets->load_or_create_user(
+    Name     => 'user',
+    Password => 'password',
+);
+
+my ( $baseurl, $m ) = RT::Test->started_ok;
+
+#set up lifecycle for catalog 2
+ok $m->login;
+$m->get_ok( '/Admin/Assets/Catalogs/Modify.html?id=' . $catalog_2->id );
+$m->form_name('ModifyCatalog');
+$m->submit_form( fields => { Lifecycle => 'foo' } );
+
+# set up custom field on catalog 2
+my $cf = RT::Test::Assets::create_cf( Name => 'test_cf', Catalog => $catalog_2->Name, Type => 'FreeformSingle' );
+$cf->AddToObject( $catalog_2 );
+ok( $cf, "custom field created (id:" . $cf->Id . ")" );
+my $cf_form_id    = 'Object-RT::Asset--CustomField-' . $cf->Id . '-Value';
+my $cf_test_value = "some string for test_cf $$";
+
+# load initial asset create page without specifying catalog
+# should have default catalog with no custom fields
+note('load create asset page with defaults');
+$m->get_ok('/Asset/Create.html?');
+
+ok( !$m->form_name('CreateAsset')->find_input($cf_form_id), 'custom field not present' );
+is( $m->form_name('CreateAsset')->value('Catalog'), $catalog_1->id, 'Catalog selection dropdown populated and pre-selected' );
+is( $m->form_name('CreateAsset')->value('Status'), 'new', 'Status selection dropdown populated and pre-selected' );
+
+$m->get_ok( '/Asset/Create.html', 'go to asset create page with no catalog id' );
+ok( !$m->form_name('CreateAsset')->find_input($cf_form_id), 'custom field not present' );
+is( $m->form_name('CreateAsset')->value('Catalog'), $catalog_1->id, 'Catalog selection dropdown populated and pre-selected' );
+is( $m->form_name('CreateAsset')->value('Status'), 'new', 'Status selection dropdown populated and pre-selected' );
+
+# test asset creation on reload from selected catalog, specifying catalog with custom fields
+note('reload asset create page with selected catalog');
+$m->get_ok( '/Asset/Create.html?Catalog=' . $catalog_2->id, 'go to asset create page' );
+
+is( $m->form_name('CreateAsset')->value('Catalog'), $catalog_2->id, 'Catalog selection dropdown populated and pre-selected' );
+ok( $m->form_name('CreateAsset')->find_input($cf_form_id), 'custom field present' );
+is( $m->form_name('CreateAsset')->value($cf_form_id), '', 'custom field present and empty' );
+
+my $form         = $m->form_name('CreateAsset');
+my $status_input = $form->find_input('Status');
+note( "values: " . join( ' - ', map { "[$_]" } $status_input->possible_values ) );
+is_deeply(
+    [ $status_input->possible_values ],
+    [ 'initial', 'tracked', 'retired' ],
+    'status selectbox shows custom lifecycle for queue'
+);
+note('submit populated form');
+$m->submit_form( fields => { Name => 'asset foo', 'Catalog' => $catalog_2->id, $cf_form_id => $cf_test_value } );
+$m->text_contains( 'test_cf',      'custom field populated in display' );
+$m->text_contains( $cf_test_value, 'custom field populated in display' );
+
+my $asset = RT::Test::Assets->last_asset;
+ok( $asset->id, 'asset is created' );
+is( $asset->CatalogObj->id, $catalog_2->id, 'asset created with correct catalog' );
+
+done_testing();

-----------------------------------------------------------------------


More information about the rt-commit mailing list